<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>6. Induction &mdash; The Mechanics of Proof, by Heather Macbeth</title>
      <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
      <link rel="stylesheet" href="_static/css/custom.css" type="text/css" />
    <link rel="shortcut icon" href="_static/favicon.ico"/>
  <!--[if lt IE 9]>
    <script src="_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
        <script src="_static/jquery.js"></script>
        <script src="_static/underscore.js"></script>
        <script src="_static/doctools.js"></script>
        <script async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    <script src="_static/js/theme.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="7. Number theory" href="07_Number_Theory.html" />
    <link rel="prev" title="5. Logic" href="05_Logic.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="index.html" class="icon icon-home"> The Mechanics of Proof
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
<li class="toctree-l1"><a class="reference internal" href="00_Introduction.html">Preface</a></li>
</ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="01_Proofs_by_Calculation.html">1. Proofs by calculation</a></li>
<li class="toctree-l1"><a class="reference internal" href="02_Proofs_with_Structure.html">2. Proofs with structure</a></li>
<li class="toctree-l1"><a class="reference internal" href="03_Parity_and_Divisibility.html">3. Parity and divisibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="04_Proofs_with_Structure_II.html">4. Proofs with structure, II</a></li>
<li class="toctree-l1"><a class="reference internal" href="05_Logic.html">5. Logic</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">6. Induction</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#introduction">6.1. Introduction</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#example">6.1.1. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id2">6.1.2. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#modeq-pow-proof">6.1.3. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#induction-alternating-congruence">6.1.4. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#induction-starting-point">6.1.5. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id6">6.1.6. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#exercises">6.1.7. Exercises</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#recurrence-relations">6.2. Recurrence relations</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id7">6.2.1. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id8">6.2.2. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id9">6.2.3. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#triangle">6.2.4. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#factorial">6.2.5. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#factorial-lower-bound">6.2.6. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id13">6.2.7. Exercises</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#two-step-induction">6.3. Two-step induction</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id15">6.3.1. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id16">6.3.2. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#fibonacci">6.3.3. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id18">6.3.4. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#two-step-starting-point">6.3.5. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id21">6.3.6. Exercises</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#strong-induction">6.4. Strong induction</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id24">6.4.1. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#exists-prime-factor">6.4.2. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id26">6.4.3. Exercises</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#pascal-s-triangle">6.5. Pascal&#8217;s triangle</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#definition">6.5.1. Definition</a></li>
<li class="toctree-l3"><a class="reference internal" href="#pascal-bound">6.5.2. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id28">6.5.3. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id29">6.5.4. Exercises</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#the-division-algorithm">6.6. The Division Algorithm</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#constructive-division-algorithm-def">6.6.1. Definition</a></li>
<li class="toctree-l3"><a class="reference internal" href="#mod-add-div">6.6.2. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#mod-nonneg">6.6.3. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#mod-lt">6.6.4. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#weak-division-algorithm">6.6.5. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id35">6.6.6. Exercises</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#the-euclidean-algorithm">6.7. The Euclidean algorithm</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#euclidean-algorithm-def">6.7.1. Definition</a></li>
<li class="toctree-l3"><a class="reference internal" href="#gcd-nonneg">6.7.2. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#common-divisor">6.7.3. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#bezout-def">6.7.4. Definition</a></li>
<li class="toctree-l3"><a class="reference internal" href="#bezout-gcd-recursive">6.7.5. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#bezout-gcd">6.7.6. Example</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id42">6.7.7. Exercises</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="07_Number_Theory.html">7. Number theory</a></li>
<li class="toctree-l1"><a class="reference internal" href="08_Functions.html">8. Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="09_Sets.html">9. Sets</a></li>
<li class="toctree-l1"><a class="reference internal" href="10_Relations.html">10. Relations</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Index_of_Tactics.html">Index of Lean tactics</a></li>
<li class="toctree-l1"><a class="reference internal" href="Mainstream_Lean.html">Transitioning to mainstream Lean</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">The Mechanics of Proof</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
      <li><span class="section-number">6. </span>Induction</li>
      <li class="wy-breadcrumbs-aside">
            <a href="_sources/06_Induction.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="induction">
<span id="id1"></span><h1><span class="section-number">6. </span>Induction<a class="headerlink" href="#induction" title="Permalink to this headline">&#61633;</a></h1>
<p>This chapter introduces <em>induction</em>, a proof method which applies to the natural numbers
and to other discrete types such as integers or pairs of natural numbers.
We also introduce <em>recursion</em>, a method for defining sequences (and, more generally,
functions from discrete types); induction is the canonical method for proving results about
recursively-defined objects.</p>
<p>In <a class="reference internal" href="#induction-intro"><span class="std std-numref">Section 6.1</span></a> - <a class="reference internal" href="#two-step-induction"><span class="std std-numref">Section 6.3</span></a>,
we use only the most traditional form of induction, proving a result for a natural number by
relating it to the result for the previous natural number, and small variants on this form
of induction.  In <a class="reference internal" href="#strong-induction"><span class="std std-numref">Section 6.4</span></a> -
<a class="reference internal" href="#euclidean-algorithm"><span class="std std-numref">Section 6.7</span></a>, we introduce <em>strong induction</em>, and, even more
generally, <em>well-founded induction</em>. These induction principles are more flexible.</p>
<section id="introduction">
<span id="induction-intro"></span><h2><span class="section-number">6.1. </span>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">&#61633;</a></h2>
<section id="example">
<span id="first-induction-example"></span><h3><span class="section-number">6.1.1. </span>Example<a class="headerlink" href="#example" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number.  Show that <span class="math notranslate nohighlight">\(2 ^n\ge n+1\)</span>.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.  The base case, <span class="math notranslate nohighlight">\(2^0\geq 0+1\)</span>, is clear.</p>
<p>Suppose now that for some natural number <span class="math notranslate nohighlight">\(k\)</span>, it is true that <span class="math notranslate nohighlight">\(2 ^k\ge k+1\)</span>.  Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}2^{k+1} &amp;=  2 \cdot 2^k \\
&amp;\ge 2(k+1)\\
&amp;=(k+1+1) + k\\
&amp;\geq k+1+1.\end{split}\]</div>
</div>
<p>In Lean, the tactic <code class="docutils literal notranslate"><span class="pre">simple_induction</span></code> will set up an induction proof.  Here, before the line
<code class="docutils literal notranslate"><span class="pre">simple_induction</span> <span class="pre">n</span> <span class="pre">with</span> <span class="pre">k</span> <span class="pre">IH</span></code>, the goal state displays a single goal,</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>n : &#8469;
&#8866; 2 ^ n &#8805; n + 1
</pre></div>
</div>
<p>and after the tactic is used the goal state displays two goals, one for the base case and one for
the inductive step.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>&#8866; 2 ^ 0 &#8805; 0 + 1

k : &#8469;
IH : 2 ^ k &#8805; k + 1
&#8866; 2 ^ (k + 1) &#8805; k + 1 + 1
</pre></div>
</div>
<p>Here is the full proof in Lean.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8805;</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">simple_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- base case</span>
    <span class="n">numbers</span>
  <span class="bp">&#183;</span> <span class="c1">-- inductive step</span>
    <span class="k">calc</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">*</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">&#8805;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rel</span> <span class="o">[</span><span class="n">IH</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">&#8805;</span> <span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
</pre></div>
</div>
</section>
<section id="id2">
<h3><span class="section-number">6.1.2. </span>Example<a class="headerlink" href="#id2" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-theorem admonition">
<p class="admonition-title">Theorem</p>
<p>Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number.  Then <span class="math notranslate nohighlight">\(n\)</span> is either even or odd.</p>
</div>
<p>(Compare <a class="reference internal" href="03_Parity_and_Divisibility.html#even-or-odd"><span class="std std-numref">Example 3.1.9</span></a>, <a class="reference internal" href="04_Proofs_with_Structure_II.html#even-or-odd-proof"><span class="std std-numref">Example 4.2.9</span></a>.)</p>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.</p>
<p>The base case is to show that 0 is either even or odd.  We will show that it is even.  Indeed,
<span class="math notranslate nohighlight">\(0=2\cdot 0\)</span>.</p>
<p>Suppose now that for some natural number <span class="math notranslate nohighlight">\(k\)</span>, it is true that <span class="math notranslate nohighlight">\(k\)</span> is either even or
odd.</p>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(k\)</span> is even): Then there exists an integer <span class="math notranslate nohighlight">\(x\)</span> such that <span class="math notranslate nohighlight">\(k=2x\)</span>, and
so <span class="math notranslate nohighlight">\(k+1 = 2x+1\)</span>, so <span class="math notranslate nohighlight">\(k+1\)</span> is odd.</p>
<p><strong>Case 2</strong> (<span class="math notranslate nohighlight">\(k\)</span> is odd): Then there exists an integer <span class="math notranslate nohighlight">\(x\)</span> such that <span class="math notranslate nohighlight">\(k=2x+1\)</span>,
and</p>
<div class="math notranslate nohighlight">
\[\begin{split}k+1 &amp;=  (2x+1)+1 \\
&amp;= 2(x+1),\end{split}\]</div>
<p>so <span class="math notranslate nohighlight">\(k+1\)</span> is even.</p>
</div>
<p>Here is the outline of this argument in Lean.  Fill in the sorries.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">Even</span> <span class="n">n</span> <span class="bp">&#8744;</span> <span class="n">Odd</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">simple_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- base case</span>
    <span class="gr">sorry</span>
  <span class="bp">&#183;</span> <span class="c1">-- inductive step</span>
    <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">x</span><span class="o">,</span> <span class="n">hx</span><span class="o">&#10217;</span> <span class="bp">|</span> <span class="o">&#10216;</span><span class="n">x</span><span class="o">,</span> <span class="n">hx</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">IH</span>
    <span class="bp">&#183;</span> <span class="gr">sorry</span>
    <span class="bp">&#183;</span> <span class="gr">sorry</span>
</pre></div>
</div>
</section>
<section id="modeq-pow-proof">
<span id="id3"></span><h3><span class="section-number">6.1.3. </span>Example<a class="headerlink" href="#modeq-pow-proof" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-theorem admonition">
<p class="admonition-title">Theorem</p>
<p>Let <span class="math notranslate nohighlight">\(a, b, d\)</span> be integers, and suppose that <span class="math notranslate nohighlight">\(a\equiv b \mod d\)</span>.  Let <span class="math notranslate nohighlight">\(n\)</span> be a
natural number.  Then  <span class="math notranslate nohighlight">\(a^n\equiv b ^ n \mod d\)</span>.</p>
</div>
<p>This is the power rule for modular arithmetic, the lemma <code class="docutils literal notranslate"><span class="pre">Int.modEq.pow</span></code>, which we stated without
proof in <a class="reference internal" href="03_Parity_and_Divisibility.html#modeq-pow"><span class="std std-numref">Example 3.3.9</span></a>.  It is one of the lemmas which are bundled together to
form the tactic <code class="docutils literal notranslate"><span class="pre">rel</span></code>&#8217;s capability for modular arithmetic.</p>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.</p>
<p>We first note that <span class="math notranslate nohighlight">\(a^0-b^0 = d\cdot 0\)</span>, so <span class="math notranslate nohighlight">\(d\mid a^0-b^0\)</span>, so
<span class="math notranslate nohighlight">\(a^0\equiv b ^ 0 \mod d\)</span>. This is the base case.</p>
<p>Now, let <span class="math notranslate nohighlight">\(k\)</span> be a natural number, and suppose that  <span class="math notranslate nohighlight">\(a^k\equiv b ^ k \mod d\)</span>.  Then
there exists an integer <span class="math notranslate nohighlight">\(x\)</span> such that <span class="math notranslate nohighlight">\(a^k- b ^ k =dx\)</span>. Also, by hypothesis,
<span class="math notranslate nohighlight">\(a\equiv b \mod d\)</span>, so there exists an integer <span class="math notranslate nohighlight">\(y\)</span> such that <span class="math notranslate nohighlight">\(a-b=dy\)</span>.  We then
have</p>
<div class="math notranslate nohighlight">
\[\begin{split}a^{k+1}-b^{k+1} &amp;= a(a^k-b^k)+b^k(a-b) \\
&amp;= a(dx) +b^k(dy)\\
&amp;=d(ax+b^ky),\end{split}\]</div>
<p>so <span class="math notranslate nohighlight">\(d\mid a^{k+1}-b^{k+1}\)</span>, so <span class="math notranslate nohighlight">\(a^{k+1}\equiv b ^ {k+1} \mod d\)</span>.</p>
</div>
<p>Write out this proof in Lean.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">{</span><span class="n">a</span> <span class="n">b</span> <span class="n">d</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">}</span> <span class="o">(</span><span class="n">h</span> <span class="o">:</span> <span class="n">a</span> <span class="bp">&#8801;</span> <span class="n">b</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="n">d</span><span class="o">])</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">a</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="n">b</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="n">d</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</section>
<section id="induction-alternating-congruence">
<span id="id4"></span><h3><span class="section-number">6.1.4. </span>Example<a class="headerlink" href="#induction-alternating-congruence" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number.  Show that <span class="math notranslate nohighlight">\(4^n\)</span> is congruent to either <span class="math notranslate nohighlight">\(1\)</span> or
<span class="math notranslate nohighlight">\(4\)</span> modulo 15.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.  First, <span class="math notranslate nohighlight">\(4^0=1\)</span>, so <span class="math notranslate nohighlight">\(4^0\equiv 1\mod 15\)</span>.</p>
<p>Now, let <span class="math notranslate nohighlight">\(k\)</span> be a natural number, and suppose that we know that <span class="math notranslate nohighlight">\(4^k\)</span> is congruent to
either <span class="math notranslate nohighlight">\(1\)</span> or <span class="math notranslate nohighlight">\(4\)</span> modulo 15.</p>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(4^k\equiv 1\mod 15\)</span>): Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}4^{k+1}&amp;=4\cdot 4^{k}\\
&amp;\equiv 4 \cdot 1\mod 15\\
&amp;=4.\end{split}\]</div>
<p><strong>Case 2</strong> (<span class="math notranslate nohighlight">\(4^k\equiv 4\mod 15\)</span>): Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}4^{k+1}&amp;=4\cdot 4^{k}\\
&amp;\equiv 4 \cdot 4\mod 15\\
&amp;=15\cdot 1+1\\
&amp;\equiv 1\mod 15.\end{split}\]</div>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">15</span><span class="o">]</span> <span class="bp">&#8744;</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">4</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">15</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">simple_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- base case</span>
    <span class="n">left</span>
    <span class="n">numbers</span>
  <span class="bp">&#183;</span> <span class="c1">-- inductive step</span>
    <span class="n">obtain</span> <span class="n">hk</span> <span class="bp">|</span> <span class="n">hk</span> <span class="o">:=</span> <span class="n">IH</span>
    <span class="bp">&#183;</span> <span class="n">right</span>
      <span class="k">calc</span> <span class="o">(</span><span class="mi">4</span><span class="o">:</span><span class="n">&#8484;</span><span class="o">)</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="mi">4</span> <span class="bp">*</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
        <span class="n">_</span> <span class="bp">&#8801;</span> <span class="mi">4</span> <span class="bp">*</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">15</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rel</span> <span class="o">[</span><span class="n">hk</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">=</span> <span class="mi">4</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
    <span class="bp">&#183;</span> <span class="n">left</span>
      <span class="k">calc</span> <span class="o">(</span><span class="mi">4</span><span class="o">:</span><span class="n">&#8484;</span><span class="o">)</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="mi">4</span> <span class="bp">*</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
        <span class="n">_</span> <span class="bp">&#8801;</span> <span class="mi">4</span> <span class="bp">*</span> <span class="mi">4</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">15</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rel</span> <span class="o">[</span><span class="n">hk</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">=</span> <span class="mi">15</span> <span class="bp">*</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
        <span class="n">_</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">15</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
</pre></div>
</div>
</section>
<section id="induction-starting-point">
<span id="id5"></span><h3><span class="section-number">6.1.5. </span>Example<a class="headerlink" href="#induction-starting-point" title="Permalink to this headline">&#61633;</a></h3>
<p>We can also use induction to prove results about all natural numbers greater than a given number.
We just start the induction at that number.</p>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number greater than or equal to 2.  Show that <span class="math notranslate nohighlight">\(3 ^n\ge 2^n+5\)</span>.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>, starting at 2.  The base case, <span class="math notranslate nohighlight">\(3^2\geq 2^2+5\)</span>, is
clear.</p>
<p>Suppose now that for some natural number <span class="math notranslate nohighlight">\(k\)</span>, it is true that <span class="math notranslate nohighlight">\(3 ^k\ge 2^k+5\)</span>.  Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}3^{k+1} &amp;=  2 \cdot 3^k + 3^k\\
&amp;\ge 2(2^k+5)+3^k\\
&amp;=2^{k+1}+5+(5+3^k)\\
&amp;\geq 2^{k+1}+5.\end{split}\]</div>
</div>
<p>In Lean, the tactic <code class="docutils literal notranslate"><span class="pre">induction_from_starting_point</span></code> will set up an induction proof starting at a
given point.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">{</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">}</span> <span class="o">(</span><span class="n">hn</span> <span class="o">:</span> <span class="mi">2</span> <span class="bp">&#8804;</span> <span class="n">n</span><span class="o">)</span> <span class="o">:</span> <span class="o">(</span><span class="mi">3</span><span class="o">:</span><span class="n">&#8484;</span><span class="o">)</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8805;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">5</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">induction_from_starting_point</span> <span class="n">n</span><span class="o">,</span> <span class="n">hn</span> <span class="k">with</span> <span class="n">k</span> <span class="n">hk</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- base case</span>
    <span class="n">numbers</span>
  <span class="bp">&#183;</span> <span class="c1">-- inductive step</span>
    <span class="k">calc</span> <span class="o">(</span><span class="mi">3</span><span class="o">:</span><span class="n">&#8484;</span><span class="o">)</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">*</span> <span class="mi">3</span> <span class="bp">^</span> <span class="n">k</span> <span class="bp">+</span> <span class="mi">3</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">&#8805;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="o">(</span><span class="mi">2</span> <span class="bp">^</span> <span class="n">k</span> <span class="bp">+</span> <span class="mi">5</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">3</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rel</span> <span class="o">[</span><span class="n">IH</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">5</span> <span class="bp">+</span> <span class="o">(</span><span class="mi">5</span> <span class="bp">+</span> <span class="mi">3</span> <span class="bp">^</span> <span class="n">k</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">&#8805;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">5</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
</pre></div>
</div>
</section>
<section id="id6">
<h3><span class="section-number">6.1.6. </span>Example<a class="headerlink" href="#id6" title="Permalink to this headline">&#61633;</a></h3>
<p>Induction from a nonzero starting point is a particularly useful technique for &#8220;sufficiently large&#8221;
problems.</p>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Show that for all sufficiently large natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(2^n\geq n^2\)</span>.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We will show this for all natural numbers <span class="math notranslate nohighlight">\(n\geq 4\)</span>.</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>, starting at 4.  The base case, <span class="math notranslate nohighlight">\(2^4\geq 4^2\)</span>, is
clear.</p>
<p>Suppose now that for some natural number <span class="math notranslate nohighlight">\(k\geq 4\)</span>, it is true that <span class="math notranslate nohighlight">\(2 ^k\ge k^2\)</span>.
Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}2^{k+1}&amp;=2\cdot 2^k\\
&amp;\geq 2k^2\\
&amp;=k^2+k\cdot k\\
&amp;\geq k^2+4k\\
&amp;=k^2+2k+2k\\
&amp;\geq k^2+2k+2\cdot 4\\
&amp;=(k+1)^2+7\\
&amp;\geq (k+1)^2.\end{split}\]</div>
</div>
<p>Here is the outline of this argument in Lean.  Fill in the sorries.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">:</span> <span class="n">forall_sufficiently_large</span> <span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">,</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8805;</span> <span class="n">n</span> <span class="bp">^</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">dsimp</span>
  <span class="n">use</span> <span class="mi">4</span>
  <span class="n">intro</span> <span class="n">n</span> <span class="n">hn</span>
  <span class="n">induction_from_starting_point</span> <span class="n">n</span><span class="o">,</span> <span class="n">hn</span> <span class="k">with</span> <span class="n">k</span> <span class="n">hk</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- base case</span>
    <span class="gr">sorry</span>
  <span class="bp">&#183;</span> <span class="c1">-- inductive step</span>
    <span class="gr">sorry</span>
</pre></div>
</div>
</section>
<section id="exercises">
<h3><span class="section-number">6.1.7. </span>Exercises<a class="headerlink" href="#exercises" title="Permalink to this headline">&#61633;</a></h3>
<ol class="arabic">
<li><p>Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number.  Show that <span class="math notranslate nohighlight">\(3 ^n\ge n^2+n+1\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="mi">3</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8805;</span> <span class="n">n</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">+</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Let <span class="math notranslate nohighlight">\(a\geq -1\)</span> be a real number, and let <span class="math notranslate nohighlight">\(n\)</span> be a natural number.  Show that
<span class="math notranslate nohighlight">\((1+a)^n\ge 1+na\)</span>.</p>
<p>This fact is known as <em>Bernoulli&#8217;s inequality</em>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">{</span><span class="n">a</span> <span class="o">:</span> <span class="n">&#8477;</span><span class="o">}</span> <span class="o">(</span><span class="n">ha</span> <span class="o">:</span> <span class="bp">-</span><span class="mi">1</span> <span class="bp">&#8804;</span> <span class="n">a</span><span class="o">)</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="o">(</span><span class="mi">1</span> <span class="bp">+</span> <span class="n">a</span><span class="o">)</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8805;</span> <span class="mi">1</span> <span class="bp">+</span> <span class="n">n</span> <span class="bp">*</span> <span class="n">a</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number.  Show that <span class="math notranslate nohighlight">\(5^n\)</span> is congruent to either <span class="math notranslate nohighlight">\(1\)</span> or
<span class="math notranslate nohighlight">\(5\)</span> modulo 8.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="mi">5</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">8</span><span class="o">]</span> <span class="bp">&#8744;</span> <span class="mi">5</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">5</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">8</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number.  Show that <span class="math notranslate nohighlight">\(6^n\)</span> is congruent to either <span class="math notranslate nohighlight">\(1\)</span> or
<span class="math notranslate nohighlight">\(6\)</span> modulo 7.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="mi">6</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">7</span><span class="o">]</span> <span class="bp">&#8744;</span> <span class="mi">6</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">6</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">7</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number.  Show that <span class="math notranslate nohighlight">\(4^n\)</span> is congruent to <span class="math notranslate nohighlight">\(1\)</span>, <span class="math notranslate nohighlight">\(2\)</span>
or <span class="math notranslate nohighlight">\(4\)</span> modulo 7.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span>
    <span class="mi">4</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">7</span><span class="o">]</span> <span class="bp">&#8744;</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">2</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">7</span><span class="o">]</span> <span class="bp">&#8744;</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">4</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">7</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span> sufficiently large, <span class="math notranslate nohighlight">\(3^n\geq 2^n+100\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">:</span> <span class="n">forall_sufficiently_large</span> <span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">,</span> <span class="o">(</span><span class="mi">3</span><span class="o">:</span><span class="n">&#8484;</span><span class="o">)</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8805;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">100</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">dsimp</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span> sufficiently large, <span class="math notranslate nohighlight">\(2^n\geq n^2+4\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">:</span> <span class="n">forall_sufficiently_large</span> <span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">,</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8805;</span> <span class="n">n</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">+</span> <span class="mi">4</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">dsimp</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span> sufficiently large, <span class="math notranslate nohighlight">\(2^n\geq n^3\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">:</span> <span class="n">forall_sufficiently_large</span> <span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">,</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&#8805;</span> <span class="n">n</span> <span class="bp">^</span> <span class="mi">3</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">dsimp</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Let <span class="math notranslate nohighlight">\(a\)</span> be an odd natural number.  Show by induction that for all natural numbers
<span class="math notranslate nohighlight">\(n\)</span>, the natural number <span class="math notranslate nohighlight">\(a^n\)</span> is odd.</p>
<p>Also deduce that for all natural numbers <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(n\)</span>, if <span class="math notranslate nohighlight">\(a^n\)</span> is even then
<span class="math notranslate nohighlight">\(a\)</span> is even.  (This part is not an induction problem.)</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">Odd.pow</span> <span class="o">{</span><span class="n">a</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">}</span> <span class="o">(</span><span class="n">ha</span> <span class="o">:</span> <span class="n">Odd</span> <span class="n">a</span><span class="o">)</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">Odd</span> <span class="o">(</span><span class="n">a</span> <span class="bp">^</span> <span class="n">n</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>

<span class="kd">theorem</span> <span class="n">Nat.even_of_pow_even</span> <span class="o">{</span><span class="n">a</span> <span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">}</span> <span class="o">(</span><span class="n">ha</span> <span class="o">:</span> <span class="n">Even</span> <span class="o">(</span><span class="n">a</span> <span class="bp">^</span> <span class="n">n</span><span class="o">))</span> <span class="o">:</span> <span class="n">Even</span> <span class="n">a</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
</ol>
</section>
</section>
<section id="recurrence-relations">
<span id="recurrences"></span><h2><span class="section-number">6.2. </span>Recurrence relations<a class="headerlink" href="#recurrence-relations" title="Permalink to this headline">&#61633;</a></h2>
<section id="id7">
<h3><span class="section-number">6.2.1. </span>Example<a class="headerlink" href="#id7" title="Permalink to this headline">&#61633;</a></h3>
<p>A <em>sequence</em> of numbers is an indexed list which goes on forever.  Some sequences are defined by
closed formulas.  For example, <span class="math notranslate nohighlight">\(a_n=2^n\)</span> defines a sequence, the powers of two, with</p>
<div class="math notranslate nohighlight">
\[\begin{split}a_0&amp;=1\\
a_1&amp;=2\\
a_2&amp;=4\\
a_3&amp;=8\\
a_4&amp;=16\\
a_5&amp;=32\\
\ldots\end{split}\]</div>
<p>In Lean, we would define this sequence as</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">a</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="o">:=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span>
</pre></div>
</div>
<p>and Lean will also calculate any term of the sequence we wish:</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="k">#eval</span> <span class="n">a</span> <span class="mi">20</span> <span class="c1">-- infoview displays `1048576`</span>
</pre></div>
</div>
<p>However, many important sequences do not have an easy closed formula.  A more flexible way to define
sequences is with a <em>recursive definition</em>.  For example, we can define a sequence <span class="math notranslate nohighlight">\((b_n)\)</span>
recursively by,</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}b_0&amp;=3 \\
\text{for }n:\mathbb{N},\quad b_{n+1} &amp;= b_n{}^2-2.\end{split}\]</div>
</div></blockquote>
<p>The first few terms of this sequence are</p>
<div class="math notranslate nohighlight">
\[\begin{split}b_0&amp;=3\\
b_1&amp;=b_0{}^2-2\\
&amp;=3^2-2\\
&amp;=7\\
b_2&amp;=b_1{}^2-2\\
&amp;=7^2-2\\
&amp;=47\\
b_3&amp;=b_2{}^2-2\\
&amp;=47^2-2\\
&amp;=2207\\
\ldots\end{split}\]</div>
<p>In Lean, we would define this sequence as</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">3</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="n">b</span> <span class="n">n</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">-</span> <span class="mi">2</span>
</pre></div>
</div>
<p>and Lean will also calculate any term of the sequence we wish (up to the limits of its computational
power!):</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="k">#eval</span> <span class="n">b</span> <span class="mi">7</span> <span class="c1">-- infoview displays `316837008400094222150776738483768236006420971486980607`</span>
</pre></div>
</div>
<p>When a sequence is defined recursively, it is convenient to reason about it by induction.</p>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Show that for all <span class="math notranslate nohighlight">\(n\)</span>, the integer <span class="math notranslate nohighlight">\(b_n\)</span> is odd.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We will prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.</p>
<p>First, note that</p>
<div class="math notranslate nohighlight">
\[\begin{split}b_0 &amp;= 3\\
&amp;=2\cdot 1+1,\\\end{split}\]</div>
<p>so <span class="math notranslate nohighlight">\(b_0\)</span> is odd.</p>
<p>Now, let <span class="math notranslate nohighlight">\(k\)</span> be a natural number and suppose that <span class="math notranslate nohighlight">\(b_k\)</span> is odd.  Then there exists an
integer <span class="math notranslate nohighlight">\(x\)</span> such that <span class="math notranslate nohighlight">\(b_k=2x+1\)</span>.  We then have,</p>
<div class="math notranslate nohighlight">
\[\begin{split}b_{k+1} &amp;= b_k{}^2-2\\
&amp;=(2x+1)^2-2\\
&amp;=2(2x^2+2x-1)+1,\end{split}\]</div>
<p>so <span class="math notranslate nohighlight">\(b_{k+1}\)</span> is also odd.</p>
</div>
<p>Here is that solution in Lean; note the use of <code class="docutils literal notranslate"><span class="pre">rw</span> <span class="pre">[b]</span></code> to unfold either piece of the recursive
definition of <span class="math notranslate nohighlight">\(b\)</span>, as needed.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">Odd</span> <span class="o">(</span><span class="n">b</span> <span class="n">n</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">simple_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">hk</span>
  <span class="bp">&#183;</span> <span class="c1">-- base case</span>
    <span class="n">use</span> <span class="mi">1</span>
    <span class="k">calc</span> <span class="n">b</span> <span class="mi">0</span> <span class="bp">=</span> <span class="mi">3</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">b</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">*</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="bp">&#183;</span> <span class="c1">-- inductive step</span>
    <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">x</span><span class="o">,</span> <span class="n">hx</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">hk</span>
    <span class="n">use</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">x</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">x</span> <span class="bp">-</span> <span class="mi">1</span>
    <span class="k">calc</span> <span class="n">b</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="n">b</span> <span class="n">k</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">-</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">b</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="mi">2</span> <span class="bp">*</span> <span class="n">x</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">-</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">hx</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">*</span> <span class="o">(</span><span class="mi">2</span> <span class="bp">*</span> <span class="n">x</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">x</span> <span class="bp">-</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
</pre></div>
</div>
<p>You might like to try giving another proof using the modular-arithmetic characterization of parity;
this will work both in text and in Lean.</p>
</section>
<section id="id8">
<h3><span class="section-number">6.2.2. </span>Example<a class="headerlink" href="#id8" title="Permalink to this headline">&#61633;</a></h3>
<p>Here is another recursively defined sequence:</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}x_0&amp;=5 \\
\text{for }n:\mathbb{N},\quad x_{n+1} &amp;= 2x_n-1.\end{split}\]</div>
</div></blockquote>
<p>In Lean the definition looks like this:</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">x</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">5</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">x</span> <span class="n">n</span> <span class="bp">-</span> <span class="mi">1</span>
</pre></div>
</div>
<p>Work out the first few terms of this sequence (or get Lean to do it for you!).  Here is a property
we can prove about the sequence <span class="math notranslate nohighlight">\((x_n)\)</span>:</p>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(x_n\equiv 1\mod 4\)</span>.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.</p>
<p>For the base case, observe that</p>
<div class="math notranslate nohighlight">
\[\begin{split}x_0&amp;=5\\
&amp;=4\cdot 1+1\\
&amp;\equiv 1 \mod 4.\end{split}\]</div>
<p>For the inductive step, suppose that <span class="math notranslate nohighlight">\(x_k\equiv 1\mod 4\)</span> for some natural number <span class="math notranslate nohighlight">\(k\)</span>.
Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}x_{k+1}&amp;=2x_k-1\\
&amp;\equiv 2 \cdot 1-1\mod 4\\
&amp;=1\end{split}\]</div>
<p>also.</p>
</div>
<p>Write out the two parts of the proof of this statement in Lean.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">x</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">4</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">simple_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- base case</span>
    <span class="gr">sorry</span>
  <span class="bp">&#183;</span> <span class="c1">-- inductive step</span>
    <span class="gr">sorry</span>
</pre></div>
</div>
</section>
<section id="id9">
<h3><span class="section-number">6.2.3. </span>Example<a class="headerlink" href="#id9" title="Permalink to this headline">&#61633;</a></h3>
<p>Sometimes, a sequence defined recursively can <em>also</em> be given a closed form expression.  This is the
case for the sequence <span class="math notranslate nohighlight">\((x_n)\)</span> from the previous problem.</p>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(x_n=2^{n+2}+1\)</span>.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.</p>
<p>For the base case, note that as desired,</p>
<div class="math notranslate nohighlight">
\[\begin{split}x_0&amp;= 5\\
&amp;=2^{0+2}+1.\end{split}\]</div>
<p>For the inductive step, suppose that <span class="math notranslate nohighlight">\(x_k=2^{k+2}+1\)</span> for some natural number <span class="math notranslate nohighlight">\(k\)</span>.
Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}x_{k+1}&amp;=2x_k-1\\
&amp;=2(2^{k+2}+1)-1\\
&amp;=2^{(k+1)+2}+1.\end{split}\]</div>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">x</span> <span class="n">n</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">simple_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- base case</span>
    <span class="k">calc</span> <span class="n">x</span> <span class="mi">0</span> <span class="bp">=</span> <span class="mi">5</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">x</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="mi">0</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="bp">&#183;</span> <span class="c1">-- inductive step</span>
    <span class="k">calc</span> <span class="n">x</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">x</span> <span class="n">k</span> <span class="bp">-</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">x</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">*</span> <span class="o">(</span><span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">-</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">IH</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">((</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
</pre></div>
</div>
</section>
<section id="triangle">
<span id="id10"></span><h3><span class="section-number">6.2.4. </span>Example<a class="headerlink" href="#triangle" title="Permalink to this headline">&#61633;</a></h3>
<p>Here is one more recursively defined sequence:</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}A_0&amp;=0 \\
\text{for }n:\mathbb{N},\quad A_{n+1} &amp;= A_n + (n + 1).\end{split}\]</div>
</div></blockquote>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">A</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8474;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">0</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="n">A</span> <span class="n">n</span> <span class="bp">+</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span>
</pre></div>
</div>
<p>Let&#8217;s work out the first terms of this sequence:</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}A_0&amp;=0 \\
A_1&amp;=A_0+1 \\
&amp;=1\\
A_2&amp;=A_1+2 \\
&amp;=1+2\\
&amp;=3\\
A_3&amp;=A_2+3 \\
&amp;=3+3\\
&amp;=6\\
A_4&amp;=A_3+4 \\
&amp;=6+4\\
&amp;=10\\
\ldots\end{split}\]</div>
</div></blockquote>
<p>Note the pattern: first we added 1, then 2, then 3, then 4.  So in fact</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}A_1&amp;=1 \\
A_2&amp;=1+2 \\
A_3&amp;=1+2+3 \\
A_4&amp;=1+2+3+4\\
\ldots\end{split}\]</div>
</div></blockquote>
<p>The term <span class="math notranslate nohighlight">\(A_n\)</span> of the sequence represents the sum of the numbers from 1 to <span class="math notranslate nohighlight">\(n\)</span>.</p>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>,</p>
<div class="math notranslate nohighlight">
\[A_n=\frac{n(n+1)}{2}.\]</div>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.  First note that</p>
<div class="math notranslate nohighlight">
\[\begin{split}A_0&amp;=0\\
&amp;=\frac{0(0+1)}{2}.\end{split}\]</div>
<p>This establishes the base case.  Now, let <span class="math notranslate nohighlight">\(k\)</span> be a natural number and suppose that
<span class="math notranslate nohighlight">\(A_k=\frac{k(k+1)}{2}\)</span>.  We then have</p>
<div class="math notranslate nohighlight">
\[\begin{split}A_{k+1}&amp;=A_k+(k+1)\\
&amp;=\frac{k(k+1)}{2}+(k+1)\\
&amp;=\frac{k(k+1)+2(k+1)}{2}\\
&amp;=\frac{(k+1)\cdot[(k+1)+1]}{2}.\end{split}\]</div>
</div>
<p>Here it is in Lean, written with one fewer step because Lean is better at algebra than humans are.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">A</span> <span class="n">n</span> <span class="bp">=</span> <span class="n">n</span> <span class="bp">*</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">/</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">simple_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- base case</span>
    <span class="k">calc</span> <span class="n">A</span> <span class="mi">0</span> <span class="bp">=</span> <span class="mi">0</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">A</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">0</span> <span class="bp">*</span> <span class="o">(</span><span class="mi">0</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">/</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="bp">&#183;</span> <span class="c1">-- inductive step</span>
    <span class="k">calc</span>
      <span class="n">A</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="n">A</span> <span class="n">k</span> <span class="bp">+</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">A</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">k</span> <span class="bp">*</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">/</span> <span class="mi">2</span> <span class="bp">+</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">IH</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">/</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
</pre></div>
</div>
</section>
<section id="factorial">
<span id="id11"></span><h3><span class="section-number">6.2.5. </span>Example<a class="headerlink" href="#factorial" title="Permalink to this headline">&#61633;</a></h3>
<p>We built the previous sequence by adding 1, then 2, then 3, etc.  What if we do the same thing but
for multiplication?  This gives the so-called <em>factorial</em> function, with &#8220;<span class="math notranslate nohighlight">\(n\)</span> factorial&#8221;
denoted <span class="math notranslate nohighlight">\(n!\)</span>.</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}0!&amp;=1 \\
\text{for }n:\mathbb{N},\quad(n+1)! &amp;= (n + 1) &#11037; n!\end{split}\]</div>
</div></blockquote>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">factorial</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8469;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">1</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="n">factorial</span> <span class="n">n</span>

<span class="kd">notation</span><span class="o">:</span><span class="mi">10000</span> <span class="n">n</span> <span class="s2">&quot;!&quot;</span> <span class="bp">=&gt;</span> <span class="n">factorial</span> <span class="n">n</span>
</pre></div>
</div>
<p>So</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}1!&amp;=1 \\
2!&amp;=2\cdot 1 \\
3!&amp;=3\cdot 2\cdot 1 \\
4!&amp;=4\cdot 3\cdot 2\cdot 1.\\
\ldots\end{split}\]</div>
</div></blockquote>
<p>Concretely,</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}0!&amp;=1 \\
1!&amp;=1\cdot A_0 \\
&amp;=1\cdot 1 \\
&amp;=1\\
2!&amp;=2\cdot A_1 \\
&amp;=2\cdot 1 \\
&amp;=2\\
3!&amp;=3\cdot A_2 \\
&amp;=3\cdot 2 \\
&amp;=6\\
4!&amp;=4\cdot A_3 \\
&amp;=4\cdot 6 \\
&amp;=24\\
\ldots\end{split}\]</div>
</div></blockquote>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number.  Show that every natural number <span class="math notranslate nohighlight">\(d\)</span> with
<span class="math notranslate nohighlight">\(1\le d\le n\)</span> is a factor of <span class="math notranslate nohighlight">\(n!\)</span>.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.  For the base case, <span class="math notranslate nohighlight">\(n=0\)</span>, the statement is
vacuous, since there is no natural number <span class="math notranslate nohighlight">\(d\)</span> with <span class="math notranslate nohighlight">\(1\le d\le 0\)</span>.</p>
<p>Let <span class="math notranslate nohighlight">\(k\)</span> be a natural number and suppose that every natural number <span class="math notranslate nohighlight">\(d\)</span> with
<span class="math notranslate nohighlight">\(1\le d\le k\)</span> is a factor of <span class="math notranslate nohighlight">\(k!\)</span>.  Now let <span class="math notranslate nohighlight">\(d\)</span> be a natural number with
<span class="math notranslate nohighlight">\(1\le d\le k+1\)</span>.  We must show that <span class="math notranslate nohighlight">\(d\)</span> is a factor of <span class="math notranslate nohighlight">\((k+1)!\)</span>.</p>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(d=k+1\)</span>): We have that</p>
<div class="math notranslate nohighlight">
\[\begin{split}(k+1)!&amp;=(k+1)\cdot k!\\
&amp;=d\cdot k!,\end{split}\]</div>
<p>so <span class="math notranslate nohighlight">\(d\)</span> is a factor of <span class="math notranslate nohighlight">\((k+1)!\)</span>.</p>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(d&lt;k+1\)</span>): Then <span class="math notranslate nohighlight">\(d\le k\)</span>, so by the inductive hypothesis <span class="math notranslate nohighlight">\(d\)</span> is a
factor of <span class="math notranslate nohighlight">\(k!\)</span>.  Therefore there exists a natural number <span class="math notranslate nohighlight">\(x\)</span> such that <span class="math notranslate nohighlight">\(k!=dx\)</span>.
We then have</p>
<div class="math notranslate nohighlight">
\[\begin{split}(k+1)!&amp;=(k+1)\cdot k!\\
&amp;=(k+1)\cdot dx\\
&amp;=d\cdot (k+1)x,\end{split}\]</div>
<p>so <span class="math notranslate nohighlight">\(d\)</span> is a factor of <span class="math notranslate nohighlight">\((k+1)!\)</span>.</p>
</div>
<p>Here is the same proof in Lean.  We record it for future use under the name <code class="docutils literal notranslate"><span class="pre">dvd_factorial</span></code>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="bp">&#8704;</span> <span class="n">d</span><span class="o">,</span> <span class="mi">1</span> <span class="bp">&#8804;</span> <span class="n">d</span> <span class="bp">&#8594;</span> <span class="n">d</span> <span class="bp">&#8804;</span> <span class="n">n</span> <span class="bp">&#8594;</span> <span class="n">d</span> <span class="bp">&#8739;</span> <span class="n">n</span> <span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">simple_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- base case</span>
    <span class="n">intro</span> <span class="n">k</span> <span class="n">hk1</span> <span class="n">hk</span>
    <span class="n">interval_cases</span> <span class="n">k</span>
  <span class="bp">&#183;</span> <span class="c1">-- inductive step</span>
    <span class="n">intro</span> <span class="n">d</span> <span class="n">hk1</span> <span class="n">hk</span>
    <span class="n">obtain</span> <span class="n">hk</span> <span class="bp">|</span> <span class="n">hk</span> <span class="o">:</span> <span class="n">d</span> <span class="bp">=</span> <span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">&#8744;</span> <span class="n">d</span> <span class="bp">&lt;</span> <span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="n">eq_or_lt_of_le</span> <span class="n">hk</span>
    <span class="bp">&#183;</span> <span class="c1">-- case 1: `d = k + 1`</span>
      <span class="gr">sorry</span>
    <span class="bp">&#183;</span> <span class="c1">-- case 2: `d &lt; k + 1`</span>
      <span class="gr">sorry</span>
</pre></div>
</div>
</section>
<section id="factorial-lower-bound">
<span id="id12"></span><h3><span class="section-number">6.2.6. </span>Example<a class="headerlink" href="#factorial-lower-bound" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, we have <span class="math notranslate nohighlight">\((n+1)!\ge 2^n\)</span>.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.</p>
<p>For the base case,</p>
<div class="math notranslate nohighlight">
\[\begin{split}(0+1)!&amp;=(0+1)\cdot 0!\\
&amp;= (0+1)\cdot 1\\
&amp;\ge 2 ^ 0.\end{split}\]</div>
<p>For the inductive step, suppose that for some natural number <span class="math notranslate nohighlight">\(k\)</span>, <span class="math notranslate nohighlight">\((k+1)!\ge 2^k\)</span>.
Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}(k+1+1)!&amp;=(k+1+1)\cdot (k+1)!\\
&amp;\ge(k+1+1)\cdot 2^k\\
&amp;= k\cdot 2^k+2\cdot 2^k\\
&amp;\ge 2\cdot 2^k\\
&amp;=2 ^ {k+1}.\end{split}\]</div>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="bp">&#8805;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</section>
<section id="id13">
<h3><span class="section-number">6.2.7. </span>Exercises<a class="headerlink" href="#id13" title="Permalink to this headline">&#61633;</a></h3>
<ol class="arabic">
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((c_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}c_0&amp;=7 \\
\text{for }n:\mathbb{N},\quad c_{n+1} &amp;= 3c_n-10.\end{split}\]</div>
<p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, the integer <span class="math notranslate nohighlight">\(c_n\)</span> is odd.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">c</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">7</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">3</span> <span class="bp">*</span> <span class="n">c</span> <span class="n">n</span> <span class="bp">-</span> <span class="mi">10</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">Odd</span> <span class="o">(</span><span class="n">c</span> <span class="n">n</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Let the sequence <span class="math notranslate nohighlight">\((c_n)\)</span> be defined as in the previous problem.  Show that for all
<span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(c_n=2\cdot 3^n+5\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">c</span> <span class="n">n</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">*</span> <span class="mi">3</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">5</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((y_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}y_0&amp;=2 \\
\text{for }n:\mathbb{N},\quad y_{n+1} &amp;= y_n{}^2.\end{split}\]</div>
<p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(y_n=2^{2^n}\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">y</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8469;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">2</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="o">(</span><span class="n">y</span> <span class="n">n</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">2</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">y</span> <span class="n">n</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((B_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}B_0&amp;=0 \\
\text{for }n:\mathbb{N},\quad B_{n+1} &amp;= B_n+(n+1)^2.\end{split}\]</div>
<p>Thus <span class="math notranslate nohighlight">\(B_n\)</span> represents the sum <span class="math notranslate nohighlight">\(1^2+2^2+3^2+\cdots+n^2\)</span>.
Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>,</p>
<div class="math notranslate nohighlight">
\[B_n=\frac{n(n+1)(2n+1)}{6}.\]</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">B</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8474;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">0</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="n">B</span> <span class="n">n</span> <span class="bp">+</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:</span> <span class="n">&#8474;</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">2</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">B</span> <span class="n">n</span> <span class="bp">=</span> <span class="n">n</span> <span class="bp">*</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="mi">2</span> <span class="bp">*</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">/</span> <span class="mi">6</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((S_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}S_0&amp;=1 \\
\text{for }n:\mathbb{N},\quad S_{n+1} &amp;= S_n+\frac{1}{2^{n+1}}.\end{split}\]</div>
<p>Thus <span class="math notranslate nohighlight">\(S_n\)</span> represents the sum <span class="math notranslate nohighlight">\(1+\frac{1}{2}+\frac{1}{4}+\cdots+\frac{1}{2^n}\)</span>.
Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>,</p>
<div class="math notranslate nohighlight">
\[S_n=2-\frac{1}{2^n}.\]</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">S</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8474;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">1</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="n">S</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">/</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">S</span> <span class="n">n</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">-</span> <span class="mi">1</span> <span class="bp">/</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Show that <span class="math notranslate nohighlight">\(n!\)</span> is strictly positive, for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>.</p>
<p>We record this for future use under the name <code class="docutils literal notranslate"><span class="pre">factorial_pos</span></code>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">n</span> <span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Show that <span class="math notranslate nohighlight">\(n!\)</span> is even for all <span class="math notranslate nohighlight">\(n\geq 2\)</span>.  Use induction from the starting point 2
(see <a class="reference internal" href="#induction-starting-point"><span class="std std-numref">Example 6.1.5</span></a>).</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">{</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">}</span> <span class="o">(</span><span class="n">hn</span> <span class="o">:</span> <span class="mi">2</span> <span class="bp">&#8804;</span> <span class="n">n</span><span class="o">)</span> <span class="o">:</span> <span class="n">Nat.Even</span> <span class="o">(</span><span class="n">n</span> <span class="bp">!</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\((n+1)!\le (n+1)^n\)</span>.</p>
<p>(Compare with <a class="reference internal" href="#factorial-lower-bound"><span class="std std-numref">Example 6.2.6</span></a>.)</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">!</span> <span class="bp">&#8804;</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
</ol>
</section>
</section>
<section id="two-step-induction">
<span id="id14"></span><h2><span class="section-number">6.3. </span>Two-step induction<a class="headerlink" href="#two-step-induction" title="Permalink to this headline">&#61633;</a></h2>
<section id="id15">
<h3><span class="section-number">6.3.1. </span>Example<a class="headerlink" href="#id15" title="Permalink to this headline">&#61633;</a></h3>
<p>In the last section we studied recursively defined sequences in which each term is constructed from
the previous term.  But it is also possible to define a sequence recursively with reliance on several
previous terms.</p>
<p>For example, here is a sequence defined recursively in terms of the two previous terms.</p>
<div class="math notranslate nohighlight">
\[\begin{split}a_0&amp;=2\\
a_1&amp;=1\\
\text{for }n:\mathbb{N},\quad a_{n+2}&amp;=a_{n+1}+2a_n.\end{split}\]</div>
<p>Notice that since the recurrence relation depends on two previous terms, we need to give two
concrete values of the sequence (<span class="math notranslate nohighlight">\(a_0=2\)</span> and <span class="math notranslate nohighlight">\(a_1=1\)</span>) to start with.</p>
<p>The first few terms of this sequence are</p>
<div class="math notranslate nohighlight">
\[\begin{split}a_0&amp;=2\\
a_1&amp;=1\\
a_2&amp;=a_1+2a_0\\
&amp;=1+2\cdot 2\\
&amp;=5\\
a_3&amp;=a_2+2a_1\\
&amp;=5+2\cdot 1\\
&amp;=7\\
a_4&amp;=a_3+2a_2\\
&amp;=7+2\cdot 5\\
&amp;=17\end{split}\]</div>
<p>In Lean, we would define this sequence as</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">a</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">2</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">1</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">=&gt;</span> <span class="n">a</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">a</span> <span class="n">n</span>
</pre></div>
</div>
<p>and, as in the previous section, Lean will calculate for us any term of the sequence:</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="k">#eval</span> <span class="n">a</span> <span class="mi">5</span> <span class="c1">-- infoview displays `31`</span>
</pre></div>
</div>
<p>Compute several more terms of the sequence, either on paper or with Lean&#8217;s help.  You will start to
see a pattern: every term of the sequence differs by one from a power of two.  We can prove this
pattern by induction.</p>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Prove that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(a_n=2^n+(-1)^n\)</span>.</p>
</div>
<p>In the proof below, notice that there are <em>two</em> base cases and <em>two</em> inductive hypotheses.  Think
about why.</p>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.</p>
<p>We have that</p>
<div class="math notranslate nohighlight">
\[\begin{split}a_0&amp;=2\\
&amp;=2^0+(-1)^0\end{split}\]</div>
<p>and</p>
<div class="math notranslate nohighlight">
\[\begin{split}a_1&amp;=1\\
&amp;=2^1+(-1)^1.\end{split}\]</div>
<p>Now let <span class="math notranslate nohighlight">\(k\)</span> be a natural number and suppose that <span class="math notranslate nohighlight">\(a_k=2^k+(-1)^k\)</span> and
<span class="math notranslate nohighlight">\(a_{k+1}=2^{k+1}+(-1)^{k+1}\)</span>.  Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}a_{k+2}&amp;=a_{k+1}+2a_k\\
  &amp;=(2^{k+1}+(-1)^{k+1})+2(2^k+(-1)^k)\\
  &amp;=(2\cdot 2^{k}-(-1)^{k})+(2\cdot 2^k+2\cdot (-1)^k)\\
  &amp;=2^2\cdot 2^{k}+(-1)^2\cdot (-1)^{k}\\
  &amp;=2^{k+2}+(-1)^{k+2}.\end{split}\]</div>
</div>
<p>The first two steps (using the recurrence relation and the inductive hypotheses) of the main
calculation are fairly fixed, but you might have more or fewer lines of endgame depending on your
facility with exponent rules and mental arithmetic.  Lean can do it all in one line!</p>
<p>We use the Lean tactic <code class="docutils literal notranslate"><span class="pre">two_step_induction</span></code> for this kind of induction with two base cases and two
inductive hypotheses.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">a</span> <span class="n">n</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">+</span> <span class="o">(</span><span class="bp">-</span><span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">two_step_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">IH1</span> <span class="n">IH2</span>
  <span class="bp">.</span> <span class="k">calc</span> <span class="n">a</span> <span class="mi">0</span> <span class="bp">=</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">a</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="mi">0</span> <span class="bp">+</span> <span class="o">(</span><span class="bp">-</span><span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">0</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="bp">.</span> <span class="k">calc</span> <span class="n">a</span> <span class="mi">1</span> <span class="bp">=</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">a</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="mi">1</span> <span class="bp">+</span> <span class="o">(</span><span class="bp">-</span><span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="k">calc</span>
    <span class="n">a</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span>
      <span class="bp">=</span> <span class="n">a</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">a</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">a</span><span class="o">]</span>
    <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="o">(</span><span class="bp">-</span><span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">))</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="o">(</span><span class="mi">2</span> <span class="bp">^</span> <span class="n">k</span> <span class="bp">+</span> <span class="o">(</span><span class="bp">-</span><span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="n">k</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">IH1</span><span class="o">,</span> <span class="n">IH2</span><span class="o">]</span>
    <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="mi">2</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="bp">+</span> <span class="o">(</span><span class="bp">-</span><span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
</pre></div>
</div>
</section>
<section id="id16">
<h3><span class="section-number">6.3.2. </span>Example<a class="headerlink" href="#id16" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Prove that for all natural numbers <span class="math notranslate nohighlight">\(m\geq 1\)</span>, <span class="math notranslate nohighlight">\(a_m\)</span> is congruent to either 1 or 5
modulo 6.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We will prove a more precise result than stated, namely that for all natural numbers
<span class="math notranslate nohighlight">\(n\geq 1\)</span>, either</p>
<ul class="simple">
<li><p><span class="math notranslate nohighlight">\(a_n\equiv 1\mod 6\)</span> and <span class="math notranslate nohighlight">\(a_{n+1}\equiv 5\mod 6\)</span>, or</p></li>
<li><p><span class="math notranslate nohighlight">\(a_n\equiv 5\mod 6\)</span> and <span class="math notranslate nohighlight">\(a_{n+1}\equiv 1\mod 6\)</span>.</p></li>
</ul>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.</p>
<p>For the base case, <span class="math notranslate nohighlight">\(n=1\)</span>, notice that <span class="math notranslate nohighlight">\(a_1=1\)</span> and <span class="math notranslate nohighlight">\(a_2=5\)</span>, so
<span class="math notranslate nohighlight">\(a_1\equiv 1\mod 6\)</span> and <span class="math notranslate nohighlight">\(a_2\equiv 5\mod 6\)</span>.</p>
<p>For the inductive step, let <span class="math notranslate nohighlight">\(k\)</span> be a natural number and suppose that either</p>
<ul class="simple">
<li><p><span class="math notranslate nohighlight">\(a_k\equiv 1\mod 6\)</span> and <span class="math notranslate nohighlight">\(a_{k+1}\equiv 5\mod 6\)</span>, or</p></li>
<li><p><span class="math notranslate nohighlight">\(a_k\equiv 5\mod 6\)</span> and <span class="math notranslate nohighlight">\(a_{k+1}\equiv 1\mod 6\)</span>.</p></li>
</ul>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(a_k\equiv 1\mod 6\)</span> and <span class="math notranslate nohighlight">\(a_{k+1}\equiv 5\mod 6\)</span>): Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}a_{k+2}&amp;=a_{k+1}+2a_k\\
&amp;\equiv 5+2\cdot 1\mod 6\\
&amp;=6\cdot 1+1\\
&amp;\equiv 1\mod 6.\end{split}\]</div>
<p><strong>Case 2</strong> (<span class="math notranslate nohighlight">\(a_k\equiv 5\mod 6\)</span> and <span class="math notranslate nohighlight">\(a_{k+1}\equiv 1\mod 6\)</span>): Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}a_{k+2}&amp;=a_{k+1}+2a_k\\
&amp;\equiv 1+2\cdot 5\mod 6\\
&amp;=6\cdot 1+5\\
&amp;\equiv 5\mod 6.\end{split}\]</div>
</div>
<p>It may not be clear to you exactly why these calculations are what&#8217;s needed to solve the problem.
There is also quite a bit of low-level logical manipulation happening, without being called out
directly in the text.  The Lean proof below may make some of these logical manipulations more
apparent to you.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">{</span><span class="n">m</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">}</span> <span class="o">(</span><span class="n">hm</span> <span class="o">:</span> <span class="mi">1</span> <span class="bp">&#8804;</span> <span class="n">m</span><span class="o">)</span> <span class="o">:</span> <span class="n">a</span> <span class="n">m</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">]</span> <span class="bp">&#8744;</span> <span class="n">a</span> <span class="n">m</span> <span class="bp">&#8801;</span> <span class="mi">5</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="k">have</span> <span class="n">H</span> <span class="o">:</span> <span class="bp">&#8704;</span> <span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">,</span> <span class="mi">1</span> <span class="bp">&#8804;</span> <span class="n">n</span> <span class="bp">&#8594;</span>
      <span class="o">(</span><span class="n">a</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">]</span> <span class="bp">&#8743;</span> <span class="n">a</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">&#8801;</span> <span class="mi">5</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">])</span>
    <span class="bp">&#8744;</span> <span class="o">(</span><span class="n">a</span> <span class="n">n</span> <span class="bp">&#8801;</span> <span class="mi">5</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">]</span> <span class="bp">&#8743;</span> <span class="n">a</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">])</span>
  <span class="bp">&#183;</span> <span class="n">intro</span> <span class="n">n</span> <span class="n">hn</span>
    <span class="n">induction_from_starting_point</span> <span class="n">n</span><span class="o">,</span> <span class="n">hn</span> <span class="k">with</span> <span class="n">k</span> <span class="n">hk</span> <span class="n">IH</span>
    <span class="bp">&#183;</span> <span class="n">left</span>
      <span class="n">constructor</span>
      <span class="k">calc</span> <span class="n">a</span> <span class="mi">1</span> <span class="bp">=</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">a</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
      <span class="k">calc</span> <span class="n">a</span> <span class="o">(</span><span class="mi">1</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">a</span><span class="o">,</span> <span class="n">a</span><span class="o">,</span> <span class="n">a</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">=</span> <span class="mi">5</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
        <span class="n">_</span> <span class="bp">&#8801;</span> <span class="mi">5</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
    <span class="bp">&#183;</span> <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">IH1</span><span class="o">,</span> <span class="n">IH2</span><span class="o">&#10217;</span> <span class="bp">|</span> <span class="o">&#10216;</span><span class="n">IH1</span><span class="o">,</span> <span class="n">IH2</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">IH</span>
      <span class="bp">&#183;</span> <span class="n">right</span>
        <span class="n">constructor</span>
        <span class="bp">&#183;</span> <span class="n">apply</span> <span class="n">IH2</span>
        <span class="k">calc</span> <span class="n">a</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="n">a</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">a</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">a</span><span class="o">]</span>
          <span class="n">_</span> <span class="bp">&#8801;</span> <span class="mi">5</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rel</span> <span class="o">[</span><span class="n">IH1</span><span class="o">,</span> <span class="n">IH2</span><span class="o">]</span>
          <span class="n">_</span> <span class="bp">=</span> <span class="mi">6</span> <span class="bp">*</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
          <span class="n">_</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
      <span class="bp">&#183;</span> <span class="n">left</span>
        <span class="n">constructor</span>
        <span class="bp">&#183;</span> <span class="n">apply</span> <span class="n">IH2</span>
        <span class="k">calc</span> <span class="n">a</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="n">a</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">a</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">a</span><span class="o">]</span>
          <span class="n">_</span> <span class="bp">&#8801;</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="mi">5</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rel</span> <span class="o">[</span><span class="n">IH1</span><span class="o">,</span> <span class="n">IH2</span><span class="o">]</span>
          <span class="n">_</span> <span class="bp">=</span> <span class="mi">6</span> <span class="bp">*</span> <span class="mi">1</span> <span class="bp">+</span> <span class="mi">5</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
          <span class="n">_</span> <span class="bp">&#8801;</span> <span class="mi">5</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">6</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
  <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">H1</span><span class="o">,</span> <span class="n">H2</span><span class="o">&#10217;</span> <span class="bp">|</span> <span class="o">&#10216;</span><span class="n">H1</span><span class="o">,</span> <span class="n">H2</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">H</span> <span class="n">m</span> <span class="n">hm</span>
  <span class="bp">&#183;</span> <span class="n">left</span>
    <span class="n">apply</span> <span class="n">H1</span>
  <span class="bp">&#183;</span> <span class="n">right</span>
    <span class="n">apply</span> <span class="n">H1</span>
</pre></div>
</div>
</section>
<section id="fibonacci">
<span id="id17"></span><h3><span class="section-number">6.3.3. </span>Example<a class="headerlink" href="#fibonacci" title="Permalink to this headline">&#61633;</a></h3>
<p>The most famous example of a sequence defined recursively in terms of its two previous values is the
<em>Fibonacci sequence</em>: each term is the sum of the two previous.</p>
<div class="math notranslate nohighlight">
\[\begin{split}F_0&amp;=1\\
F_1&amp;=1\\
\text{for }n:\mathbb{N},\quad F_{n+2}&amp;=F_{n+1}+F_n.\end{split}\]</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">F</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">1</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">1</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">=&gt;</span> <span class="n">F</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="n">F</span> <span class="n">n</span>
</pre></div>
</div>
<p>Work out the first 10 terms, on paper or with Lean&#8217;s help.</p>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Show that the Fibonacci sequence <span class="math notranslate nohighlight">\((F_n)\)</span> satisfies: for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>,
<span class="math notranslate nohighlight">\(F_n \le 2^n\)</span>.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.</p>
<p>For <span class="math notranslate nohighlight">\(n=0\)</span>, we have that</p>
<div class="math notranslate nohighlight">
\[\begin{split}F_0&amp;=1\\
&amp;\le 2^0.\end{split}\]</div>
<p>For <span class="math notranslate nohighlight">\(n=1\)</span>, we have that</p>
<div class="math notranslate nohighlight">
\[\begin{split}F_1&amp;=1\\
&amp;\le 2^1.\end{split}\]</div>
<p>Let <span class="math notranslate nohighlight">\(k\)</span> be a natural number and suppose that <span class="math notranslate nohighlight">\(F_k\le 2^k\)</span> and
<span class="math notranslate nohighlight">\(F_{k+1}\le 2^{k+1}\)</span>.  Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}F_{k+2}&amp;=F_{k+1}+F_k\\
&amp;\le 2^{k+1}+2^k\\
&amp;\le 2^{k+1}+2^k+2^k\\
&amp;= 2^{k+1}+2^{k+1}\\
&amp;= 2^{k+2}.\end{split}\]</div>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">F</span> <span class="n">n</span> <span class="bp">&#8804;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">two_step_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">IH1</span> <span class="n">IH2</span>
  <span class="bp">&#183;</span> <span class="k">calc</span> <span class="n">F</span> <span class="mi">0</span> <span class="bp">=</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">F</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">&#8804;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="mi">0</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="bp">&#183;</span> <span class="k">calc</span> <span class="n">F</span> <span class="mi">1</span> <span class="bp">=</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">F</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">&#8804;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="bp">&#183;</span> <span class="k">calc</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="bp">=</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="n">F</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">F</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">&#8804;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rel</span> <span class="o">[</span><span class="n">IH1</span><span class="o">,</span> <span class="n">IH2</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">&#8804;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
</pre></div>
</div>
</section>
<section id="id18">
<h3><span class="section-number">6.3.4. </span>Example<a class="headerlink" href="#id18" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Show <a class="footnote-reference brackets" href="#id22" id="id19">1</a> that the Fibonacci sequence <span class="math notranslate nohighlight">\((F_n)\)</span> satisfies: for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>,</p>
<div class="math notranslate nohighlight">
\[F_{n+1}^2-F_{n+1}F_n-F_n^2=-(-1)^n.\]</div>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by induction on <span class="math notranslate nohighlight">\(n\)</span>.  First,</p>
<div class="math notranslate nohighlight">
\[\begin{split}F_1^2-F_1F_0-F_0^2&amp;=1^2-1\cdot 1-1^2\\
&amp;=-(-1)^0.\end{split}\]</div>
<p>Now, let <span class="math notranslate nohighlight">\(k\)</span> be a natural number and suppose that
<span class="math notranslate nohighlight">\(F_{k+1}^2-F_{k+1}F_k-F_k^2=-(-1)^k\)</span>. Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}F_{k+2}^2-F_{k+2}F_{k+1}-F_{k+1}^2
&amp;=(F_{k+1}+F_k)^2-(F_{k+1}+F_k)F_{k+1}-F_{k+1}^2\\
&amp;=-(F_{k+1}^2-F_{k+1}F_k-F_k^2)\\
&amp;=-(-(-1)^k)\\
&amp;=-(-1)^{k+1}.\end{split}\]</div>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">F</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">-</span> <span class="n">F</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="n">F</span> <span class="n">n</span> <span class="bp">-</span> <span class="n">F</span> <span class="n">n</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">=</span> <span class="bp">-</span> <span class="o">(</span><span class="bp">-</span><span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">simple_induction</span> <span class="n">n</span> <span class="k">with</span> <span class="n">k</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="k">calc</span> <span class="n">F</span> <span class="mi">1</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">-</span> <span class="n">F</span> <span class="mi">1</span> <span class="bp">*</span> <span class="n">F</span> <span class="mi">0</span> <span class="bp">-</span> <span class="n">F</span> <span class="mi">0</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">=</span> <span class="mi">1</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">-</span> <span class="mi">1</span> <span class="bp">*</span> <span class="mi">1</span> <span class="bp">-</span> <span class="mi">1</span> <span class="bp">^</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">F</span><span class="o">,</span> <span class="n">F</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="bp">-</span> <span class="o">(</span><span class="bp">-</span><span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">0</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="bp">&#183;</span> <span class="k">calc</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">-</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="bp">*</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">-</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">2</span>
        <span class="bp">=</span> <span class="o">(</span><span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="n">F</span> <span class="n">k</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">-</span> <span class="o">(</span><span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="n">F</span> <span class="n">k</span><span class="o">)</span> <span class="bp">*</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span>
            <span class="bp">-</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">F</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="bp">-</span> <span class="o">(</span><span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">2</span> <span class="bp">-</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="n">F</span> <span class="n">k</span> <span class="bp">-</span> <span class="n">F</span> <span class="n">k</span> <span class="bp">^</span> <span class="mi">2</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="bp">-</span> <span class="bp">-</span><span class="o">(</span><span class="bp">-</span><span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">IH</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="bp">-</span><span class="o">(</span><span class="bp">-</span><span class="mi">1</span><span class="o">)</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
</pre></div>
</div>
</section>
<section id="two-step-starting-point">
<span id="id20"></span><h3><span class="section-number">6.3.5. </span>Example<a class="headerlink" href="#two-step-starting-point" title="Permalink to this headline">&#61633;</a></h3>
<p>We have so far seen simple induction, induction from a specified starting point, and two-step
induction. It may not surprise you to learn that it is also valid to perform two-step induction from
a specified starting point.</p>
<p>Consider the sequence <span class="math notranslate nohighlight">\((d_n)\)</span> defined recursively by,</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}d_0&amp;=3\\
d_1&amp;=1\\
\text{for }n:\mathbb{N},\quad d_{n+2}&amp;=3d_{n+1}+5d_n.\end{split}\]</div>
</div></blockquote>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">d</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">3</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">1</span>
  <span class="bp">|</span> <span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">=&gt;</span> <span class="mi">3</span> <span class="bp">*</span> <span class="n">d</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">5</span> <span class="bp">*</span> <span class="n">d</span> <span class="n">k</span>
</pre></div>
</div>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Show that for all sufficiently large natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(d_n \ge 4^n\)</span>.</p>
</div>
<p>To start this problem, you might experiment by calculating the first few terms, by hand or in Lean.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="k">#eval</span> <span class="n">d</span> <span class="mi">2</span> <span class="c1">-- infoview displays `18`</span>
<span class="k">#eval</span> <span class="n">d</span> <span class="mi">3</span> <span class="c1">-- infoview displays `59`</span>
<span class="k">#eval</span> <span class="n">d</span> <span class="mi">4</span> <span class="c1">-- infoview displays `267`</span>
<span class="k">#eval</span> <span class="n">d</span> <span class="mi">5</span> <span class="c1">-- infoview displays `1096`</span>
<span class="k">#eval</span> <span class="n">d</span> <span class="mi">6</span> <span class="c1">-- infoview displays `4623`</span>
<span class="k">#eval</span> <span class="n">d</span> <span class="mi">7</span> <span class="c1">-- infoview displays `19349`</span>
</pre></div>
</div>
<p>Likewise, you can calculate the first few powers of 4, by hand or in Lean.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="k">#eval</span> <span class="mi">4</span> <span class="bp">^</span> <span class="mi">2</span> <span class="c1">-- infoview displays `16`</span>
<span class="k">#eval</span> <span class="mi">4</span> <span class="bp">^</span> <span class="mi">3</span> <span class="c1">-- infoview displays `64`</span>
<span class="k">#eval</span> <span class="mi">4</span> <span class="bp">^</span> <span class="mi">4</span> <span class="c1">-- infoview displays `256`</span>
<span class="k">#eval</span> <span class="mi">4</span> <span class="bp">^</span> <span class="mi">5</span> <span class="c1">-- infoview displays `1024`</span>
<span class="k">#eval</span> <span class="mi">4</span> <span class="bp">^</span> <span class="mi">6</span> <span class="c1">-- infoview displays `4096`</span>
<span class="k">#eval</span> <span class="mi">4</span> <span class="bp">^</span> <span class="mi">7</span> <span class="c1">-- infoview displays `16384`</span>
</pre></div>
</div>
<p>Based on this limited sample, it looks like <span class="math notranslate nohighlight">\(d_n\)</span> overtakes <span class="math notranslate nohighlight">\(4^n\)</span> at <span class="math notranslate nohighlight">\(n=4\)</span>.  So
let&#8217;s try an induction starting at 4.</p>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We will show this for all natural numbers <span class="math notranslate nohighlight">\(n\geq 4\)</span>.</p>
<p>For <span class="math notranslate nohighlight">\(n=4\)</span>, we have that</p>
<div class="math notranslate nohighlight">
\[\begin{split}d_4&amp;=267\\
&amp;\ge 4^4.\end{split}\]</div>
<p>For <span class="math notranslate nohighlight">\(n=5\)</span>, we have that</p>
<div class="math notranslate nohighlight">
\[\begin{split}d_5&amp;=1096\\
&amp;\ge 4^5.\end{split}\]</div>
<p>Let <span class="math notranslate nohighlight">\(k\)</span> be a natural number and suppose that <span class="math notranslate nohighlight">\(d_k\ge 4^k\)</span> and
<span class="math notranslate nohighlight">\(d_{k+1}\ge 4^{k+1}\)</span>.  Then we have that</p>
<div class="math notranslate nohighlight">
\[\begin{split}d_{k+2}&amp;=3d_{k+1}+5d_k\\
&amp;\ge 3 \cdot 4^{k+1}+5 \cdot 4^k\\
&amp;= 16 \cdot 4^k + 4 ^ k\\
&amp;\geq 16 \cdot 4^k\\
&amp;= 4^{k+2}.\end{split}\]</div>
</div>
<p>In Lean, we can use the tactic <code class="docutils literal notranslate"><span class="pre">two_step_induction_from_starting_point</span></code> for this style of
argument.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">:</span> <span class="n">forall_sufficiently_large</span> <span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">,</span> <span class="n">d</span> <span class="n">n</span> <span class="bp">&#8805;</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">dsimp</span>
  <span class="n">use</span> <span class="mi">4</span>
  <span class="n">intro</span> <span class="n">n</span> <span class="n">hn</span>
  <span class="n">two_step_induction_from_starting_point</span> <span class="n">n</span><span class="o">,</span> <span class="n">hn</span> <span class="k">with</span> <span class="n">k</span> <span class="n">hk</span> <span class="n">IH1</span> <span class="n">IH2</span>
  <span class="bp">&#183;</span> <span class="k">calc</span> <span class="n">d</span> <span class="mi">4</span> <span class="bp">=</span> <span class="mi">267</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rfl</span>
      <span class="n">_</span> <span class="bp">&#8805;</span> <span class="mi">4</span> <span class="bp">^</span> <span class="mi">4</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="bp">&#183;</span> <span class="k">calc</span> <span class="n">d</span> <span class="mi">5</span> <span class="bp">=</span> <span class="mi">1096</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rfl</span>
      <span class="n">_</span> <span class="bp">&#8805;</span> <span class="mi">4</span> <span class="bp">^</span> <span class="mi">5</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="k">calc</span> <span class="n">d</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="bp">=</span> <span class="mi">3</span> <span class="bp">*</span> <span class="n">d</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">5</span> <span class="bp">*</span> <span class="n">d</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">d</span><span class="o">]</span>
    <span class="n">_</span> <span class="bp">&#8805;</span> <span class="mi">3</span> <span class="bp">*</span> <span class="mi">4</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">5</span> <span class="bp">*</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rel</span> <span class="o">[</span><span class="n">IH1</span><span class="o">,</span> <span class="n">IH2</span><span class="o">]</span>
    <span class="n">_</span> <span class="bp">=</span> <span class="mi">16</span> <span class="bp">*</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">k</span> <span class="bp">+</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
    <span class="n">_</span> <span class="bp">&#8805;</span> <span class="mi">16</span> <span class="bp">*</span> <span class="mi">4</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
    <span class="n">_</span> <span class="bp">=</span> <span class="mi">4</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
</pre></div>
</div>
</section>
<section id="id21">
<h3><span class="section-number">6.3.6. </span>Exercises<a class="headerlink" href="#id21" title="Permalink to this headline">&#61633;</a></h3>
<ol class="arabic">
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((b_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}b_0&amp;=0\\
b_1&amp;=1\\
\text{for }n:\mathbb{N},\quad b_{n+2}&amp;=5b_{n+1}-6b_n.\end{split}\]</div>
<p>Prove that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(b_n=3^n - 2 ^ n\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">0</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">1</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">=&gt;</span> <span class="mi">5</span> <span class="bp">*</span> <span class="n">b</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">-</span> <span class="mi">6</span> <span class="bp">*</span> <span class="n">b</span> <span class="n">n</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">b</span> <span class="n">n</span> <span class="bp">=</span> <span class="mi">3</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">-</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((c_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}c_0&amp;=3\\
c_1&amp;=2\\
\text{for }n:\mathbb{N},\quad c_{n+2}&amp;=4c_n.\end{split}\]</div>
<p>Prove that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(c_n=2\cdot 2^n+(-2)^n\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">c</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">3</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">2</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">=&gt;</span> <span class="mi">4</span> <span class="bp">*</span> <span class="n">c</span> <span class="n">n</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">c</span> <span class="n">n</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">*</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">+</span> <span class="o">(</span><span class="bp">-</span><span class="mi">2</span><span class="o">)</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((t_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}t_0&amp;=5\\
t_1&amp;=7\\
\text{for }n:\mathbb{N},\quad t_{n+2}&amp;=2t_{n+1}-t_n.\end{split}\]</div>
<p>Prove that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(t_n=2n+5\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">t</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">5</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">7</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">=&gt;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">t</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">-</span> <span class="n">t</span> <span class="n">n</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">t</span> <span class="n">n</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">5</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((q_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}q_0&amp;=1\\
q_1&amp;=2\\
\text{for }n:\mathbb{N},\quad q_{n+2}&amp;=2q_{n+1}-q_n+6n + 6.\end{split}\]</div>
<p>Prove that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(q_n=n^3+1\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">q</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">1</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">2</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">=&gt;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">q</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">-</span> <span class="n">q</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">6</span> <span class="bp">*</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">6</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">q</span> <span class="n">n</span> <span class="bp">=</span> <span class="o">(</span><span class="n">n</span><span class="o">:</span><span class="n">&#8484;</span><span class="o">)</span> <span class="bp">^</span> <span class="mi">3</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((s_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}s_0&amp;=2\\
s_1&amp;=3\\
\text{for }n:\mathbb{N},\quad s_{n+2}&amp;=2s_{n+1}+3s_n.\end{split}\]</div>
<p>Prove that for all natural numbers <span class="math notranslate nohighlight">\(m\)</span>, <span class="math notranslate nohighlight">\(s_m\)</span> is congruent to either 2 or 3
modulo 5.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">s</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">2</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">3</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">=&gt;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">s</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">3</span> <span class="bp">*</span> <span class="n">s</span> <span class="n">n</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">m</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">s</span> <span class="n">m</span> <span class="bp">&#8801;</span> <span class="mi">2</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">5</span><span class="o">]</span> <span class="bp">&#8744;</span> <span class="n">s</span> <span class="n">m</span> <span class="bp">&#8801;</span> <span class="mi">3</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">5</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((p_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}p_0&amp;=2\\
p_1&amp;=3\\
\text{for }n:\mathbb{N},\quad p_{n+2}&amp;=6p_{n+1}-p_n.\end{split}\]</div>
<p>Prove that for all natural numbers <span class="math notranslate nohighlight">\(m\geq 1\)</span>, <span class="math notranslate nohighlight">\(p_m\)</span> is congruent to either 2 or 3
modulo 7.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">p</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">2</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">3</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">=&gt;</span> <span class="mi">6</span> <span class="bp">*</span> <span class="n">p</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">-</span> <span class="n">p</span> <span class="n">n</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">m</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">p</span> <span class="n">m</span> <span class="bp">&#8801;</span> <span class="mi">2</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">7</span><span class="o">]</span> <span class="bp">&#8744;</span> <span class="n">p</span> <span class="n">m</span> <span class="bp">&#8801;</span> <span class="mi">3</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="mi">7</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Consider the sequence <span class="math notranslate nohighlight">\((r_n)\)</span> defined recursively by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}r_0&amp;=2\\
r_1&amp;=0\\
\text{for }n:\mathbb{N},\quad r_{n+2}&amp;=2r_{n+1}+r_n.\end{split}\]</div>
<p>Prove that for all sufficiently large natural numbers <span class="math notranslate nohighlight">\(n\)</span>, it is true that
<span class="math notranslate nohighlight">\(r_n\geq 2^n\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">r</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8484;</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">2</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">0</span>
  <span class="bp">|</span> <span class="n">n</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">=&gt;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">r</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="n">r</span> <span class="n">n</span>

<span class="kd">example</span> <span class="o">:</span> <span class="n">forall_sufficiently_large</span> <span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">,</span> <span class="n">r</span> <span class="n">n</span> <span class="bp">&#8805;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Show that the Fibonacci sequence <span class="math notranslate nohighlight">\((F_n)\)</span> satisfies: for all sufficiently large natural
numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(0.4 \cdot 1.6^n &lt; F_n &lt; 0.5 \cdot 1.7^n\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">:</span> <span class="n">forall_sufficiently_large</span> <span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">,</span>
    <span class="o">(</span><span class="mi">0</span><span class="bp">.</span><span class="mi">4</span><span class="o">:</span><span class="n">&#8474;</span><span class="o">)</span> <span class="bp">*</span> <span class="mi">1</span><span class="bp">.</span><span class="mi">6</span> <span class="bp">^</span> <span class="n">n</span> <span class="bp">&lt;</span> <span class="n">F</span> <span class="n">n</span> <span class="bp">&#8743;</span> <span class="n">F</span> <span class="n">n</span> <span class="bp">&lt;</span> <span class="o">(</span><span class="mi">0</span><span class="bp">.</span><span class="mi">5</span><span class="o">:</span><span class="n">&#8474;</span><span class="o">)</span> <span class="bp">*</span> <span class="mi">1</span><span class="bp">.</span><span class="mi">7</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
</ol>
<p class="rubric">Footnotes</p>
<dl class="footnote brackets">
<dt class="label" id="id22"><span class="brackets"><a class="fn-backref" href="#id19">1</a></span></dt>
<dd><p>Example adapted from Hammack,
<a class="reference external" href="https://www.people.vcu.edu/~rhammack/BookOfProof/">Book of Proof</a>, Section 10.5.</p>
</dd>
</dl>
</section>
</section>
<section id="strong-induction">
<span id="id23"></span><h2><span class="section-number">6.4. </span>Strong induction<a class="headerlink" href="#strong-induction" title="Permalink to this headline">&#61633;</a></h2>
<section id="id24">
<h3><span class="section-number">6.4.1. </span>Example<a class="headerlink" href="#id24" title="Permalink to this headline">&#61633;</a></h3>
<p>We have been encountering increasingly complicated induction principles, starting from &#8220;simple
induction&#8221; in <a class="reference internal" href="#first-induction-example"><span class="std std-numref">Example 6.1.1</span></a> and eventually reaching rather niche
induction principles like &#8220;two-step induction from a specified starting point&#8221; in
<a class="reference internal" href="#two-step-starting-point"><span class="std std-numref">Example 6.3.5</span></a>.  Rather than develop more and more exotic induction
principles as our problems become yet more complicated, let&#8217;s explain a more general method: <em>strong
induction</em>.  This method lets us prove a proposition for the natural numbers one by one, relying at
each step not just on the immediately previous step, but on <em>any</em> previous step.</p>
<p>Let&#8217;s re-do <a class="reference internal" href="#fibonacci"><span class="std std-numref">Example 6.3.3</span></a> using strong induction.  The differences
will be more apparent in Lean, but we start with a text proof, where the difference amounts to a
change of emphasis.</p>
<div class="admonition-problem admonition">
<p class="admonition-title">Problem</p>
<p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(F_n \le 2^n\)</span>.</p>
</div>
<div class="admonition-solution admonition">
<p class="admonition-title">Solution</p>
<p>We prove this by strong induction on <span class="math notranslate nohighlight">\(n\)</span>. Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number and suppose that
for all natural numbers <span class="math notranslate nohighlight">\(m &lt; n\)</span>, <span class="math notranslate nohighlight">\(F_m \le 2^m\)</span>. <span class="math notranslate nohighlight">\((\star)\)</span></p>
<p>We consider cases according to whether <span class="math notranslate nohighlight">\(n\)</span> is 0, 1, or <span class="math notranslate nohighlight">\(k + 2\)</span> for some
natural number <span class="math notranslate nohighlight">\(k\)</span>.</p>
<p>For <span class="math notranslate nohighlight">\(n=0\)</span>, we have that</p>
<div class="math notranslate nohighlight">
\[\begin{split}F_0&amp;=1\\
&amp;\le 2^0.\end{split}\]</div>
<p>For <span class="math notranslate nohighlight">\(n=1\)</span>, we have that</p>
<div class="math notranslate nohighlight">
\[\begin{split}F_1&amp;=1\\
&amp;\le 2^1.\end{split}\]</div>
<p>For <span class="math notranslate nohighlight">\(n=k+2\)</span>, we have that <span class="math notranslate nohighlight">\(k&lt;k+2\)</span> and <span class="math notranslate nohighlight">\(k+1&lt;k+2\)</span>, so by the inductive hypothesis
<span class="math notranslate nohighlight">\((\star)\)</span>, <span class="math notranslate nohighlight">\(F_k\le 2^k\)</span> and <span class="math notranslate nohighlight">\(F_{k+1}\le 2^{k+1}\)</span>.  Thus</p>
<div class="math notranslate nohighlight">
\[\begin{split}F_{k+2}&amp;=F_{k+1}+F_k\\
&amp;\le 2^{k+1}+2^k\\
&amp;\le 2^{k+1}+2^k+2^k\\
&amp;= 2^{k+1}+2^{k+1}\\
&amp;= 2^{k+2}.\end{split}\]</div>
</div>
<p>In Lean, strong induction can be used in a proof almost silently.  We set up a theorem
stating the result we want to prove by strong induction (here the statement</p>
<blockquote>
<div><p>for all natural numbers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(F_n \le 2^n\)</span></p>
</div></blockquote>
<p>which I have named <code class="docutils literal notranslate"><span class="pre">F_bound</span></code> in Lean).  Then within the proof of that theorem we can refer to the
theorem itself!  Lean will attempt to check for us that we are using the theorem only for input
values smaller than the value currently being studied.</p>
<p>You might find this suspicious, or in danger of becoming circular.  Check for yourself that if you
try to invoke the lemma <code class="docutils literal notranslate"><span class="pre">F_bound</span></code> at the value <span class="math notranslate nohighlight">\(n\)</span> itself, or at a larger value like
<span class="math notranslate nohighlight">\(n + 17\)</span>, then Lean gives an error.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">F_bound</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">F</span> <span class="n">n</span> <span class="bp">&#8804;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="k">match</span> <span class="n">n</span> <span class="k">with</span>
  <span class="bp">|</span> <span class="mi">0</span> <span class="bp">=&gt;</span>
      <span class="k">calc</span> <span class="n">F</span> <span class="mi">0</span> <span class="bp">=</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">F</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">&#8804;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="mi">0</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="bp">|</span> <span class="mi">1</span> <span class="bp">=&gt;</span>
      <span class="k">calc</span> <span class="n">F</span> <span class="mi">1</span> <span class="bp">=</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">F</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">&#8804;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">numbers</span>
  <span class="bp">|</span> <span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span>  <span class="bp">=&gt;</span>
      <span class="k">have</span> <span class="n">IH1</span> <span class="o">:=</span> <span class="n">F_bound</span> <span class="n">k</span> <span class="c1">-- first inductive hypothesis</span>
      <span class="k">have</span> <span class="n">IH2</span> <span class="o">:=</span> <span class="n">F_bound</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="c1">-- second inductive hypothesis</span>
      <span class="k">calc</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="bp">=</span> <span class="n">F</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="n">F</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">F</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">&#8804;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rel</span> <span class="o">[</span><span class="n">IH1</span><span class="o">,</span> <span class="n">IH2</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">&#8804;</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">k</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
        <span class="n">_</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="o">(</span><span class="n">k</span> <span class="bp">+</span> <span class="mi">2</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
</pre></div>
</div>
</section>
<section id="exists-prime-factor">
<span id="id25"></span><h3><span class="section-number">6.4.2. </span>Example<a class="headerlink" href="#exists-prime-factor" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-theorem admonition">
<p class="admonition-title">Theorem</p>
<p>Let <span class="math notranslate nohighlight">\(n \geq 2\)</span> be a natural number.  Then there exists a prime number <span class="math notranslate nohighlight">\(p\)</span> which is a
factor of <span class="math notranslate nohighlight">\(n\)</span>.</p>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by strong induction on <span class="math notranslate nohighlight">\(n\)</span>. Let <span class="math notranslate nohighlight">\(n\)</span> be a natural number and suppose that
for all natural numbers <span class="math notranslate nohighlight">\(2 \le m &lt; n\)</span>, there exists a prime number <span class="math notranslate nohighlight">\(p\)</span> which is a
factor of <span class="math notranslate nohighlight">\(m\)</span>. <span class="math notranslate nohighlight">\((\star)\)</span></p>
<p>If <span class="math notranslate nohighlight">\(n\)</span> is prime, then <span class="math notranslate nohighlight">\(n\)</span> itself is a prime factor of <span class="math notranslate nohighlight">\(n\)</span>, and we are done.</p>
<p>If <span class="math notranslate nohighlight">\(n\)</span> is not prime, then since <span class="math notranslate nohighlight">\(n \geq 2\)</span>, there exists a natural number
<span class="math notranslate nohighlight">\(2 \le m &lt; n\)</span> which is a factor of <span class="math notranslate nohighlight">\(n\)</span>.  (This was proved in one of the exercises in
<a class="reference internal" href="05_Logic.html#negation-normalize"><span class="std std-numref">Section 5.3</span></a>.)  By the inductive hypothesis <span class="math notranslate nohighlight">\((\star)\)</span>, there
exists a prime number <span class="math notranslate nohighlight">\(p\)</span> which is a factor of <span class="math notranslate nohighlight">\(m\)</span>.</p>
<p>Since <span class="math notranslate nohighlight">\(m\mid n\)</span>, there exists a natural number <span class="math notranslate nohighlight">\(x\)</span> such that <span class="math notranslate nohighlight">\(n = mx\)</span>.  Since
<span class="math notranslate nohighlight">\(p \mid m\)</span>, there exists a natural number <span class="math notranslate nohighlight">\(y\)</span> such that <span class="math notranslate nohighlight">\(m = py\)</span>. Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}n &amp;=mx\\
&amp;=(py)x\\
&amp;=p(xy),\end{split}\]</div>
<p>so <span class="math notranslate nohighlight">\(p\)</span> is a factor of <span class="math notranslate nohighlight">\(n\)</span> too.</p>
</div>
<p>Here is the same proof in Lean.  The lemma from the exercise in
<a class="reference internal" href="05_Logic.html#negation-normalize"><span class="std std-numref">Section 5.3</span></a> has the Lean name <code class="docutils literal notranslate"><span class="pre">exists_factor_of_not_prime</span></code>.</p>
<p>Notice that this is again a strong induction proof: we are invoking the theorem (which we name
<code class="docutils literal notranslate"><span class="pre">exists_prime_factor</span></code>), instantiated for <span class="math notranslate nohighlight">\(m\)</span>, within the proof of the theorem instantiated
for <span class="math notranslate nohighlight">\(n\)</span>.  At that point Lean has a hypothesis available saying that <span class="math notranslate nohighlight">\(m&lt;n\)</span>, so this is
valid.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">exists_prime_factor</span> <span class="o">{</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">}</span> <span class="o">(</span><span class="n">hn2</span> <span class="o">:</span> <span class="mi">2</span> <span class="bp">&#8804;</span> <span class="n">n</span><span class="o">)</span> <span class="o">:</span> <span class="bp">&#8707;</span> <span class="n">p</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">,</span> <span class="n">Prime</span> <span class="n">p</span> <span class="bp">&#8743;</span> <span class="n">p</span> <span class="bp">&#8739;</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">by_cases</span> <span class="n">hn</span> <span class="o">:</span> <span class="n">Prime</span> <span class="n">n</span>
  <span class="bp">.</span> <span class="c1">-- case 1: `n` is prime</span>
    <span class="n">use</span> <span class="n">n</span>
    <span class="n">constructor</span>
    <span class="bp">&#183;</span> <span class="n">apply</span> <span class="n">hn</span>
    <span class="bp">&#183;</span> <span class="n">use</span> <span class="mi">1</span>
      <span class="n">ring</span>
  <span class="bp">.</span> <span class="c1">-- case 2: `n` is not prime</span>
    <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">m</span><span class="o">,</span> <span class="n">hmn</span><span class="o">,</span> <span class="n">_</span><span class="o">,</span> <span class="o">&#10216;</span><span class="n">x</span><span class="o">,</span> <span class="n">hx</span><span class="o">&#10217;&#10217;</span> <span class="o">:=</span> <span class="n">exists_factor_of_not_prime</span> <span class="n">hn</span> <span class="n">hn2</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:</span> <span class="bp">&#8707;</span> <span class="n">p</span><span class="o">,</span> <span class="n">Prime</span> <span class="n">p</span> <span class="bp">&#8743;</span> <span class="n">p</span> <span class="bp">&#8739;</span> <span class="n">m</span> <span class="o">:=</span> <span class="n">exists_prime_factor</span> <span class="n">hmn</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">p</span><span class="o">,</span> <span class="n">hp</span><span class="o">,</span> <span class="n">y</span><span class="o">,</span> <span class="n">hy</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">IH</span>
    <span class="n">use</span> <span class="n">p</span>
    <span class="n">constructor</span>
    <span class="bp">&#183;</span> <span class="n">apply</span> <span class="n">hp</span>
    <span class="bp">&#183;</span> <span class="n">use</span> <span class="n">x</span> <span class="bp">*</span> <span class="n">y</span>
      <span class="k">calc</span> <span class="n">n</span> <span class="bp">=</span> <span class="n">m</span> <span class="bp">*</span> <span class="n">x</span> <span class="o">:=</span> <span class="n">hx</span>
        <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="n">p</span> <span class="bp">*</span> <span class="n">y</span><span class="o">)</span> <span class="bp">*</span> <span class="n">x</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">hy</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">=</span> <span class="n">p</span> <span class="bp">*</span> <span class="o">(</span><span class="n">x</span> <span class="bp">*</span> <span class="n">y</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
</pre></div>
</div>
</section>
<section id="id26">
<h3><span class="section-number">6.4.3. </span>Exercises<a class="headerlink" href="#id26" title="Permalink to this headline">&#61633;</a></h3>
<ol class="arabic">
<li><p>Show that for all natural numbers <span class="math notranslate nohighlight">\(n&gt;0\)</span>, there exist natural numbers <span class="math notranslate nohighlight">\(a\)</span> and
<span class="math notranslate nohighlight">\(x\)</span>, with <span class="math notranslate nohighlight">\(x\)</span> odd, such that <span class="math notranslate nohighlight">\(n=2^ax\)</span>.</p>
<p>Suggested approach: start with a case split on the parity of <span class="math notranslate nohighlight">\(n\)</span>, using the lemma
<code class="docutils literal notranslate"><span class="pre">even_or_odd</span></code>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">extract_pow_two</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">(</span><span class="n">hn</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">n</span><span class="o">)</span> <span class="o">:</span> <span class="bp">&#8707;</span> <span class="n">a</span> <span class="n">x</span><span class="o">,</span> <span class="n">Odd</span> <span class="n">x</span> <span class="bp">&#8743;</span> <span class="n">n</span> <span class="bp">=</span> <span class="mi">2</span> <span class="bp">^</span> <span class="n">a</span> <span class="bp">*</span> <span class="n">x</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
</ol>
</section>
</section>
<section id="pascal-s-triangle">
<span id="pascal"></span><h2><span class="section-number">6.5. </span>Pascal&#8217;s triangle<a class="headerlink" href="#pascal-s-triangle" title="Permalink to this headline">&#61633;</a></h2>
<section id="definition">
<span id="pascal-def"></span><h3><span class="section-number">6.5.1. </span>Definition<a class="headerlink" href="#definition" title="Permalink to this headline">&#61633;</a></h3>
<p>Consider the family of natural numbers <span class="math notranslate nohighlight">\((P_{a,b})\)</span> defined recursively by,</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}\text{for }a:\mathbb{N},\quad P_{a,0}&amp;=1 \\
\text{for }b:\mathbb{N},\quad P_{0,b+1}&amp;=1 \\
\text{for }a,b:\mathbb{N},\quad P_{a+1,b+1} &amp;= P_{a+1,b}+P_{a,b+1}.\end{split}\]</div>
</div></blockquote>
<p>This definition is <em>well-founded</em> because each step of the definition depends only on previous terms
<span class="math notranslate nohighlight">\(P_{a,b}\)</span> for which the expression <span class="math notranslate nohighlight">\(a+b\)</span> is strictly smaller.</p>
<p>Here is how this looks in Lean, with the well-foundedness explanation expressed using the syntax
<code class="docutils literal notranslate"><span class="pre">termination_by</span></code>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">pascal</span> <span class="o">:</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8469;</span> <span class="bp">&#8594;</span> <span class="n">&#8469;</span>
  <span class="bp">|</span> <span class="n">a</span><span class="o">,</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="mi">1</span>
  <span class="bp">|</span> <span class="mi">0</span><span class="o">,</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="mi">1</span>
  <span class="bp">|</span> <span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">,</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="n">pascal</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">b</span> <span class="bp">+</span> <span class="n">pascal</span> <span class="n">a</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">a</span> <span class="bp">+</span> <span class="n">b</span>
</pre></div>
</div>
<p>As usual, Lean can work out any value of the function we ask for.  For example,</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="k">#eval</span> <span class="n">pascal</span> <span class="mi">2</span> <span class="mi">4</span> <span class="c1">-- infoview displays `15`</span>
</pre></div>
</div>
<p>Here are all the values for <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> from 0 to 5.</p>
<span id="pascal-triangle-table"></span><table class="colwidths-given docutils align-default" id="id43">
<caption><span class="caption-number">Table 6.1 </span><span class="caption-text">Values of the recursively defined function <code class="docutils literal notranslate"><span class="pre">pascal</span></code></span><a class="headerlink" href="#id43" title="Permalink to this table">&#61633;</a></caption>
<colgroup>
<col style="width: 14%" />
<col style="width: 14%" />
<col style="width: 14%" />
<col style="width: 14%" />
<col style="width: 14%" />
<col style="width: 14%" />
<col style="width: 14%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head stub"></th>
<th class="head"><p>0</p></th>
<th class="head"><p>1</p></th>
<th class="head"><p>2</p></th>
<th class="head"><p>3</p></th>
<th class="head"><p>4</p></th>
<th class="head"><p>5</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><th class="stub"><p>0</p></th>
<td><p>1</p></td>
<td><p>1</p></td>
<td><p>1</p></td>
<td><p>1</p></td>
<td><p>1</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><th class="stub"><p>1</p></th>
<td><p>1</p></td>
<td><p>2</p></td>
<td><p>3</p></td>
<td><p>4</p></td>
<td><p>5</p></td>
<td><p>6</p></td>
</tr>
<tr class="row-even"><th class="stub"><p>2</p></th>
<td><p>1</p></td>
<td><p>3</p></td>
<td><p>6</p></td>
<td><p>10</p></td>
<td><p>15</p></td>
<td><p>21</p></td>
</tr>
<tr class="row-odd"><th class="stub"><p>3</p></th>
<td><p>1</p></td>
<td><p>4</p></td>
<td><p>10</p></td>
<td><p>20</p></td>
<td><p>35</p></td>
<td><p>56</p></td>
</tr>
<tr class="row-even"><th class="stub"><p>4</p></th>
<td><p>1</p></td>
<td><p>5</p></td>
<td><p>15</p></td>
<td><p>35</p></td>
<td><p>70</p></td>
<td><p>126</p></td>
</tr>
<tr class="row-odd"><th class="stub"><p>5</p></th>
<td><p>1</p></td>
<td><p>6</p></td>
<td><p>21</p></td>
<td><p>56</p></td>
<td><p>126</p></td>
<td><p>252</p></td>
</tr>
</tbody>
</table>
<p>Check your understanding of the definition by re-calculating a few of these values from scratch.</p>
<p>The conventional visualization of the function <code class="docutils literal notranslate"><span class="pre">pascal</span></code> is in a rotated version of the above
table, as a triangle.</p>
<figure class="align-default" id="id44">
<img alt="_images/pascal.gif" src="_images/pascal.gif" />
<figcaption>
<p><span class="caption-number">Fig. 6.1 </span><span class="caption-text">Pascal&#8217;s triangle (image credit:
<a class="reference external" href="https://commons.wikimedia.org/wiki/File:PascalTriangleAnimated2.gif">Wikimedia Commons</a>)</span><a class="headerlink" href="#id44" title="Permalink to this image">&#61633;</a></p>
</figcaption>
</figure>
</section>
<section id="pascal-bound">
<span id="id27"></span><h3><span class="section-number">6.5.2. </span>Example<a class="headerlink" href="#pascal-bound" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-theorem admonition">
<p class="admonition-title">Theorem</p>
<p>For all natural numbers <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>, <span class="math notranslate nohighlight">\(P_{a,b} \le (a+b)!\)</span>.</p>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by strong induction relative to the expression <span class="math notranslate nohighlight">\(a+b\)</span>.</p>
<p>By an exercise in <a class="reference internal" href="#recurrences"><span class="std std-numref">Section 6.2</span></a>, factorials are always <span class="math notranslate nohighlight">\(\geq 1\)</span>, so
for all <span class="math notranslate nohighlight">\(a\)</span>,</p>
<div class="math notranslate nohighlight">
\[\begin{split}P_{a,0}&amp;=1\\
&amp;\le (a+0)!,\end{split}\]</div>
<p>and for all <span class="math notranslate nohighlight">\(b\)</span>,</p>
<div class="math notranslate nohighlight">
\[\begin{split}P_{0,b+1}&amp;=1\\
&amp;\le (0+(b+1))!.\end{split}\]</div>
<p>Now let <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> be natural numbers and suppose that for all <span class="math notranslate nohighlight">\(x\)</span> and
<span class="math notranslate nohighlight">\(y\)</span> with <span class="math notranslate nohighlight">\(x+y&lt;(a+1)+(b+1)\)</span>, it is true that <span class="math notranslate nohighlight">\(P_{x,y} \le (x+y)!\)</span>.  Then in
particular</p>
<div class="math notranslate nohighlight">
\[\begin{split}P_{a+1,b} &amp;\le ((a+1)+b)!\\
P_{a,b+1} &amp;\le (a+(b+1))!.\end{split}\]</div>
<p>So</p>
<div class="math notranslate nohighlight">
\[\begin{split}P_{a + 1,b + 1} &amp;= P_{a + 1, b} + P_{a, b + 1}\\
  &amp; &#8804; (a + 1 + b) ! + (a + (b + 1)) !\\
  &amp; &#8804; (a + b)  (a + b + 1) ! + (a + 1 + b) ! + (a + (b + 1)) ! \\
  &amp; = ((a + b + 1) + 1) (a + b + 1)! \\
  &amp; = ((a + b + 1) + 1)! \\
  &amp; = (a + 1 + (b + 1))!.\end{split}\]</div>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">pascal_le</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">pascal</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">&#8804;</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="k">match</span> <span class="n">a</span><span class="o">,</span> <span class="n">b</span> <span class="k">with</span>
  <span class="bp">|</span> <span class="n">a</span><span class="o">,</span> <span class="mi">0</span> <span class="bp">=&gt;</span>
      <span class="k">calc</span> <span class="n">pascal</span> <span class="n">a</span> <span class="mi">0</span> <span class="bp">=</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">pascal</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">&#8804;</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">0</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">apply</span> <span class="n">factorial_pos</span>
  <span class="bp">|</span> <span class="mi">0</span><span class="o">,</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span>
      <span class="k">calc</span> <span class="n">pascal</span> <span class="mi">0</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">pascal</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">&#8804;</span> <span class="o">(</span><span class="mi">0</span> <span class="bp">+</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">))</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">apply</span> <span class="n">factorial_pos</span>
  <span class="bp">|</span> <span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">,</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span>
      <span class="k">have</span> <span class="n">IH1</span> <span class="o">:=</span> <span class="n">pascal_le</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">b</span> <span class="c1">-- inductive hypothesis</span>
      <span class="k">have</span> <span class="n">IH2</span> <span class="o">:=</span> <span class="n">pascal_le</span> <span class="n">a</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
      <span class="k">calc</span> <span class="n">pascal</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">=</span> <span class="n">pascal</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">b</span> <span class="bp">+</span> <span class="n">pascal</span> <span class="n">a</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">pascal</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">&#8804;</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span> <span class="bp">!</span> <span class="bp">+</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">))</span> <span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rel</span> <span class="o">[</span><span class="n">IH1</span><span class="o">,</span> <span class="n">IH2</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">&#8804;</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">!</span> <span class="bp">+</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span> <span class="bp">!</span> <span class="bp">+</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">))</span> <span class="bp">!</span>  <span class="o">:=</span> <span class="kd">by</span> <span class="n">extra</span>
        <span class="n">_</span> <span class="bp">=</span> <span class="o">((</span><span class="n">a</span> <span class="bp">+</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
        <span class="n">_</span> <span class="bp">=</span> <span class="o">((</span><span class="n">a</span> <span class="bp">+</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">factorial</span><span class="o">,</span> <span class="n">factorial</span><span class="o">,</span> <span class="n">factorial</span><span class="o">]</span>
        <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">+</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">))</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">a</span> <span class="bp">+</span> <span class="n">b</span>
</pre></div>
</div>
</section>
<section id="id28">
<h3><span class="section-number">6.5.3. </span>Example<a class="headerlink" href="#id28" title="Permalink to this headline">&#61633;</a></h3>
<p>With a more delicate calculation, we can improve the bound in <a class="reference internal" href="#pascal-bound"><span class="std std-numref">Example 6.5.2</span></a> to
an exact formula.</p>
<div class="admonition-theorem admonition">
<p class="admonition-title">Theorem</p>
<p>For all natural numbers <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>, <span class="math notranslate nohighlight">\(P_{a,b} \ a! \ b!= (a+b)!\)</span>.</p>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by strong induction relative to the expression <span class="math notranslate nohighlight">\(a+b\)</span>.</p>
<p>For all <span class="math notranslate nohighlight">\(a\)</span>,</p>
<div class="math notranslate nohighlight">
\[\begin{split}P_{a,0}&amp;=1\\
&amp;= (a+0)!,\end{split}\]</div>
<p>and for all <span class="math notranslate nohighlight">\(b\)</span>,</p>
<div class="math notranslate nohighlight">
\[\begin{split}P_{0,b+1}&amp;=1\\
&amp;= (0+(b+1))!.\end{split}\]</div>
<p>Now let <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> be natural numbers and suppose that for all <span class="math notranslate nohighlight">\(x\)</span> and
<span class="math notranslate nohighlight">\(y\)</span> with <span class="math notranslate nohighlight">\(x+y&lt;(a+1)+(b+1)\)</span>, it is true that <span class="math notranslate nohighlight">\(P_{x,y} \ x! \ y! = (x+y)!\)</span>.  Then
in particular</p>
<div class="math notranslate nohighlight">
\[\begin{split}P_{a+1,b} \ (a+1)! \ b! &amp;= ((a+1)+b)!\\
P_{a,b+1} \ a! \ (b+1)! &amp;= (a+(b+1))!.\end{split}\]</div>
<p>So</p>
<div class="math notranslate nohighlight">
\[\begin{split}P_{a + 1, b + 1} \ (a + 1)! \ (b + 1)!
&amp; = (P_{a + 1, b} + P_{a, b + 1}) \ (a + 1)! \ (b + 1)! \\
&amp; = P_{a + 1, b} \ (a + 1)! \ (b + 1)!
    + P_{ a, b + 1} \ (a + 1)! \ (b + 1)! \\
&amp; = P_{a + 1, b} \ (a + 1)! \ ((b + 1)  b !)
    + P_{a, b + 1}  ((a + 1)  a !) \ (b + 1)! \\
&amp; = (b + 1) (P_{a + 1, b} \ (a + 1)! \ b !)
    + (a + 1) (P_{a, b + 1} \ a ! \ (b + 1)!) \\
&amp; = (b + 1)  ((a + 1) + b) !
    + (a + 1)  (a + (b + 1)) ! \\
&amp; = ((1 + a + b) + 1)  (1 + a + b) ! \\
&amp; = ((1 + a + b) + 1) ! \\
&amp; = ((a + 1) + (b + 1)) !.\end{split}\]</div>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">pascal_eq</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">pascal</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">!</span> <span class="bp">*</span> <span class="n">b</span> <span class="bp">!</span> <span class="bp">=</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="k">match</span> <span class="n">a</span><span class="o">,</span> <span class="n">b</span> <span class="k">with</span>
  <span class="bp">|</span> <span class="n">a</span><span class="o">,</span> <span class="mi">0</span> <span class="bp">=&gt;</span>
    <span class="k">calc</span> <span class="n">pascal</span> <span class="n">_</span> <span class="mi">0</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">!</span> <span class="bp">*</span> <span class="mi">0</span><span class="bp">!</span> <span class="bp">=</span> <span class="mi">1</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">!</span> <span class="bp">*</span> <span class="mi">0</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">pascal</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">1</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">!</span> <span class="bp">*</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">factorial</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">0</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
  <span class="bp">|</span> <span class="mi">0</span><span class="o">,</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span>
    <span class="k">calc</span> <span class="n">pascal</span> <span class="mi">0</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="mi">0</span> <span class="bp">!</span> <span class="bp">*</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="bp">=</span> <span class="mi">1</span> <span class="bp">*</span> <span class="mi">0</span> <span class="bp">!</span> <span class="bp">*</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">pascal</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="mi">1</span> <span class="bp">*</span> <span class="mi">1</span> <span class="bp">*</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">factorial</span><span class="o">,</span> <span class="n">factorial</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="mi">0</span> <span class="bp">+</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">))</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
  <span class="bp">|</span> <span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">,</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span>
    <span class="k">have</span> <span class="n">IH1</span> <span class="o">:=</span> <span class="n">pascal_eq</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">b</span> <span class="c1">-- inductive hypothesis</span>
    <span class="k">have</span> <span class="n">IH2</span> <span class="o">:=</span> <span class="n">pascal_eq</span> <span class="n">a</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
    <span class="k">calc</span>
      <span class="n">pascal</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="bp">*</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span>
        <span class="bp">=</span> <span class="o">(</span><span class="n">pascal</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">b</span> <span class="bp">+</span> <span class="n">pascal</span> <span class="n">a</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">))</span> <span class="bp">*</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="bp">*</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">pascal</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">pascal</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">b</span> <span class="bp">*</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="bp">*</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span>
          <span class="bp">+</span> <span class="n">pascal</span> <span class="n">a</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="bp">*</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">pascal</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">b</span> <span class="bp">*</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="bp">*</span> <span class="o">((</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="n">b</span> <span class="bp">!</span><span class="o">)</span>
          <span class="bp">+</span> <span class="n">pascal</span> <span class="n">a</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">((</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">!</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">factorial</span><span class="o">,</span> <span class="n">factorial</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="n">pascal</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="n">b</span> <span class="bp">*</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span> <span class="bp">*</span> <span class="n">b</span> <span class="bp">!</span><span class="o">)</span>
          <span class="bp">+</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="n">pascal</span> <span class="n">a</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">!</span> <span class="bp">*</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span><span class="bp">!</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">((</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span> <span class="bp">!</span>
          <span class="bp">+</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">))</span> <span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">IH1</span><span class="o">,</span> <span class="n">IH2</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">((</span><span class="mi">1</span> <span class="bp">+</span> <span class="n">a</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">*</span> <span class="o">(</span><span class="mi">1</span> <span class="bp">+</span> <span class="n">a</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span> <span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">((</span><span class="mi">1</span> <span class="bp">+</span> <span class="n">a</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">factorial</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">((</span><span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span> <span class="bp">+</span> <span class="o">(</span><span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">))</span> <span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">a</span> <span class="bp">+</span> <span class="n">b</span>
</pre></div>
</div>
<div class="admonition-corollary admonition">
<p class="admonition-title">Corollary</p>
<p>For all natural numbers <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>,</p>
<div class="math notranslate nohighlight">
\[P_{a,b}=\frac{(a+b)!}{a! \ b!}.\]</div>
</div>
<p>This fact is a trivial rearrangement of the theorem above, but the Lean proof is more complicated,
due to issues with division, which in this book we largely avoid.  Don&#8217;t sweat the details here,
or the two unfamiliar tactics <code class="docutils literal notranslate"><span class="pre">field_simp</span></code> and <code class="docutils literal notranslate"><span class="pre">norm_cast</span></code>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="o">(</span><span class="n">pascal</span> <span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8474;</span><span class="o">)</span> <span class="bp">=</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span><span class="bp">!</span> <span class="bp">/</span> <span class="o">(</span><span class="n">a</span> <span class="bp">!</span> <span class="bp">*</span> <span class="n">b</span> <span class="bp">!</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="k">have</span> <span class="n">ha</span> <span class="o">:=</span> <span class="n">factorial_pos</span> <span class="n">a</span>
  <span class="k">have</span> <span class="n">hb</span> <span class="o">:=</span> <span class="n">factorial_pos</span> <span class="n">b</span>
  <span class="n">field_simp</span> <span class="o">[</span><span class="n">ha</span><span class="o">,</span> <span class="n">hb</span><span class="o">]</span>
  <span class="n">norm_cast</span>
  <span class="k">calc</span> <span class="n">pascal</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">*</span> <span class="o">(</span><span class="n">a</span> <span class="bp">!</span> <span class="bp">*</span> <span class="n">b</span> <span class="bp">!</span><span class="o">)</span> <span class="bp">=</span> <span class="n">pascal</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">!</span> <span class="bp">*</span> <span class="n">b</span> <span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
    <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="n">a</span> <span class="bp">+</span> <span class="n">b</span><span class="o">)</span><span class="bp">!</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">apply</span> <span class="n">pascal_eq</span>
</pre></div>
</div>
</section>
<section id="id29">
<h3><span class="section-number">6.5.4. </span>Exercises<a class="headerlink" href="#id29" title="Permalink to this headline">&#61633;</a></h3>
<ol class="arabic">
<li><p>Prove that for all natural numbers <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>, <span class="math notranslate nohighlight">\(P_{a,b} =P_{b,a}\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">pascal_symm</span> <span class="o">(</span><span class="n">m</span> <span class="n">n</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">pascal</span> <span class="n">m</span> <span class="n">n</span> <span class="bp">=</span> <span class="n">pascal</span> <span class="n">n</span> <span class="n">m</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="k">match</span> <span class="n">m</span><span class="o">,</span> <span class="n">n</span> <span class="k">with</span>
  <span class="bp">|</span> <span class="mi">0</span><span class="o">,</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="gr">sorry</span>
  <span class="bp">|</span> <span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">,</span> <span class="mi">0</span> <span class="bp">=&gt;</span> <span class="gr">sorry</span>
  <span class="bp">|</span> <span class="mi">0</span><span class="o">,</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="gr">sorry</span>
  <span class="bp">|</span> <span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">,</span> <span class="n">b</span> <span class="bp">+</span> <span class="mi">1</span> <span class="bp">=&gt;</span> <span class="gr">sorry</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">a</span> <span class="bp">+</span> <span class="n">b</span>
</pre></div>
</div>
</li>
<li><p>Prove using simple induction that for all natural numbers <span class="math notranslate nohighlight">\(a\)</span>, <span class="math notranslate nohighlight">\(P_{a,1} =a+1\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">a</span> <span class="o">:</span> <span class="n">&#8469;</span><span class="o">)</span> <span class="o">:</span> <span class="n">pascal</span> <span class="n">a</span> <span class="mi">1</span> <span class="bp">=</span> <span class="n">a</span> <span class="bp">+</span> <span class="mi">1</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
</ol>
</section>
</section>
<section id="the-division-algorithm">
<span id="constructive-division-algorithm"></span><h2><span class="section-number">6.6. </span>The Division Algorithm<a class="headerlink" href="#the-division-algorithm" title="Permalink to this headline">&#61633;</a></h2>
<section id="constructive-division-algorithm-def">
<span id="id30"></span><h3><span class="section-number">6.6.1. </span>Definition<a class="headerlink" href="#constructive-division-algorithm-def" title="Permalink to this headline">&#61633;</a></h3>
<p>Consider the functions <span class="math notranslate nohighlight">\(\operatorname{mod}\)</span> and <span class="math notranslate nohighlight">\(\operatorname{div}\)</span> defined recursively
on the integers by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}\operatorname{mod}(n, d)&amp;=
\begin{cases}
\operatorname{mod}(n + d, d),&amp;nd&lt;0\\
\operatorname{mod}(n - d, d),&amp;0&lt; d(n-d)\\
0,&amp;n=d\\
n,&amp;0\le nd\le d^2\text{ and }n\ne d
\end{cases}\\
\operatorname{div}(n, d)&amp;=
\begin{cases}
\operatorname{div}(n + d, d)-1,&amp;nd&lt;0\\
\operatorname{div}(n - d, d)+1,&amp;0&lt; d(n-d)\\
1,&amp;n=d\\
0,&amp;0\le nd\le d^2\text{ and }n\ne d
\end{cases}\end{split}\]</div>
<p>The idea will be that <span class="math notranslate nohighlight">\(\operatorname{div}\)</span> computes the quotient of <span class="math notranslate nohighlight">\(n\)</span> by <span class="math notranslate nohighlight">\(d\)</span>
(in the elementary-school sense where we produce an integer rather than continuing into decimal
places), and <span class="math notranslate nohighlight">\(\operatorname{mod}\)</span> computes the remainder of <span class="math notranslate nohighlight">\(n\)</span> on division by
<span class="math notranslate nohighlight">\(d\)</span>.   For example,</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}&amp;
&amp;
&amp;\qquad
&amp;
&amp;\operatorname{mod}(11,4)
&amp;
&amp;\qquad
&amp;
&amp;\operatorname{div}(11,4)\\0&amp;&lt;4(11-4)
&amp;
&amp;\qquad
&amp;
&amp; =\operatorname{mod}(7,4)
&amp;
&amp;\qquad
&amp;
&amp; =\operatorname{div}(7,4)+1\\0&amp;&lt;4(7-4)
&amp;
&amp;\qquad
&amp;
&amp;=\operatorname{mod}(3,4)
&amp;
&amp;\qquad
&amp;
&amp;=(\operatorname{div}(3,4)+1)+1\\0\le 4\cdot 3\le 4^2,4&amp;\ne 3
&amp;
&amp;\qquad
&amp;
&amp;=3
&amp;
&amp;\qquad
&amp;
&amp;=(0+1)+1\\&amp;
&amp;
&amp;
&amp;
&amp;
&amp;
&amp;
&amp;
&amp;=2.\end{aligned}\end{align} \]</div>
<p>These definitions are <em>well-founded</em> because each step of the definition depends only on previous
terms <span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)\)</span>, <span class="math notranslate nohighlight">\(\operatorname{div}(n, d)\)</span> for which the expression
<span class="math notranslate nohighlight">\(2n - d\)</span> is strictly smaller in absolute value.  (This is not very obvious, although Lean can
prove it automatically.  As a sanity check,</p>
<div class="math notranslate nohighlight">
\[\begin{split}2 \cdot 11 - 4 = 18\\
2 \cdot 7 - 4 = 10\\
2 \cdot 3 - 4 = 2\end{split}\]</div>
<p>which decreases strictly.) Here is how this looks in Lean, with the well-foundedness explanation
expressed using the syntax <code class="docutils literal notranslate"><span class="pre">termination_by</span></code>, as in <a class="reference internal" href="#pascal-def"><span class="std std-numref">Definition 6.5.1</span></a>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">fmod</span> <span class="o">(</span><span class="n">n</span> <span class="n">d</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">&#8484;</span> <span class="o">:=</span>
  <span class="k">if</span> <span class="n">n</span> <span class="bp">*</span> <span class="n">d</span> <span class="bp">&lt;</span> <span class="mi">0</span> <span class="k">then</span>
    <span class="n">fmod</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span>
  <span class="k">else</span> <span class="k">if</span> <span class="n">h2</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">d</span> <span class="bp">*</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="k">then</span>
    <span class="n">fmod</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span>
  <span class="k">else</span> <span class="k">if</span> <span class="n">h3</span> <span class="o">:</span> <span class="n">n</span> <span class="bp">=</span> <span class="n">d</span> <span class="k">then</span>
    <span class="mi">0</span>
  <span class="k">else</span>
    <span class="n">n</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">n</span> <span class="n">d</span> <span class="bp">=&gt;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">n</span> <span class="bp">-</span> <span class="n">d</span>

<span class="kd">def</span> <span class="n">fdiv</span> <span class="o">(</span><span class="n">n</span> <span class="n">d</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">&#8484;</span> <span class="o">:=</span>
  <span class="k">if</span> <span class="n">n</span> <span class="bp">*</span> <span class="n">d</span> <span class="bp">&lt;</span> <span class="mi">0</span> <span class="k">then</span>
    <span class="n">fdiv</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span> <span class="bp">-</span> <span class="mi">1</span>
  <span class="k">else</span> <span class="k">if</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">d</span> <span class="bp">*</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="k">then</span>
    <span class="n">fdiv</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span> <span class="bp">+</span> <span class="mi">1</span>
  <span class="k">else</span> <span class="k">if</span> <span class="n">h3</span> <span class="o">:</span> <span class="n">n</span> <span class="bp">=</span> <span class="n">d</span> <span class="k">then</span>
    <span class="mi">1</span>
  <span class="k">else</span>
    <span class="mi">0</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">n</span> <span class="n">d</span> <span class="bp">=&gt;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">n</span> <span class="bp">-</span> <span class="n">d</span>
</pre></div>
</div>
<p>Let&#8217;s check they do what they&#8217;re supposed to:</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="k">#eval</span> <span class="n">fmod</span> <span class="mi">11</span> <span class="mi">4</span> <span class="c1">-- infoview displays `3`</span>
<span class="k">#eval</span> <span class="n">fdiv</span> <span class="mi">11</span> <span class="mi">4</span> <span class="c1">-- infoview displays `2`</span>
</pre></div>
</div>
<p>Compute a few examples yourself (checking your work with Lean) and see if you believe that
<span class="math notranslate nohighlight">\(\operatorname{div}\)</span> and <span class="math notranslate nohighlight">\(\operatorname{mod}\)</span> are producing &#8220;quotient&#8221; and &#8220;remainder&#8221;.
Now let&#8217;s make that rigorous.</p>
</section>
<section id="mod-add-div">
<span id="id31"></span><h3><span class="section-number">6.6.2. </span>Example<a class="headerlink" href="#mod-add-div" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-theorem admonition">
<p class="admonition-title">Theorem</p>
<p>For any integers <span class="math notranslate nohighlight">\(n\)</span> and <span class="math notranslate nohighlight">\(d\)</span>, we have that
<span class="math notranslate nohighlight">\(\operatorname{mod}(n, d) + d \cdot \operatorname{div}(n, d) = n\)</span>.</p>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by strong induction relative to the expression <span class="math notranslate nohighlight">\(2n - d\)</span>.  Suppose that for all
integers <span class="math notranslate nohighlight">\(m\)</span> and <span class="math notranslate nohighlight">\(c\)</span> with <span class="math notranslate nohighlight">\(|2m - c|&lt;|2n-d|\)</span>, it is true that
<span class="math notranslate nohighlight">\(\operatorname{mod}(n, d) + d \cdot \operatorname{div}(n, d) = n\)</span>.</p>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(nd&lt;0\)</span>): Then by the inductive hypothesis</p>
<div class="math notranslate nohighlight">
\[\operatorname{mod}(n+d, d) + d \cdot \operatorname{div}(n+d, d) = n+d,\]</div>
<p>so</p>
<div class="math notranslate nohighlight">
\[\begin{split}\operatorname{mod}(n, d) + d \cdot \operatorname{div}(n, d)
&amp;=\operatorname{mod}(n+d, d) + d \cdot (\operatorname{div}(n+d, d) -1)\\
&amp;=(\operatorname{mod}(n+d, d) + d \cdot \operatorname{div}(n+d, d))-d\\
&amp;=(n+d)-d\\
&amp;=n.\end{split}\]</div>
<p><strong>Case 2</strong> (<span class="math notranslate nohighlight">\(0&lt;d(n-d)\)</span>): Then by the inductive hypothesis</p>
<div class="math notranslate nohighlight">
\[\operatorname{mod}(n-d, d) + d \cdot \operatorname{div}(n-d, d) = n-d,\]</div>
<p>so</p>
<div class="math notranslate nohighlight">
\[\begin{split}\operatorname{mod}(n, d) + d \cdot \operatorname{div}(n, d)
&amp;=\operatorname{mod}(n-d, d) + d \cdot (\operatorname{div}(n-d, d) +1)\\
&amp;=(\operatorname{mod}(n-d, d) + d \cdot \operatorname{div}(n-d, d))+d\\
&amp;=(n-d)+d\\
&amp;=n.\end{split}\]</div>
<p><strong>Case 3</strong> (<span class="math notranslate nohighlight">\(n=d\)</span>): Then</p>
<div class="math notranslate nohighlight">
\[\begin{split}\operatorname{mod}(n, d) + d \cdot \operatorname{div}(n, d)
&amp;=0 + d\cdot 1\\
&amp;=d\\
&amp;=n.\end{split}\]</div>
<p><strong>Case 4</strong>: In this case,</p>
<div class="math notranslate nohighlight">
\[\begin{split}\operatorname{mod}(n, d) + d \cdot \operatorname{div}(n, d)
&amp;=n + d\cdot 0\\
&amp;=n.\end{split}\]</div>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">fmod_add_fdiv</span> <span class="o">(</span><span class="n">n</span> <span class="n">d</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">fmod</span> <span class="n">n</span> <span class="n">d</span> <span class="bp">+</span> <span class="n">d</span> <span class="bp">*</span> <span class="n">fdiv</span> <span class="n">n</span> <span class="n">d</span> <span class="bp">=</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">rw</span> <span class="o">[</span><span class="n">fdiv</span><span class="o">,</span> <span class="n">fmod</span><span class="o">]</span>
  <span class="n">split_ifs</span> <span class="k">with</span> <span class="n">h1</span> <span class="n">h2</span> <span class="n">h3</span> <span class="bp">&lt;;&gt;</span> <span class="n">push_neg</span> <span class="n">at</span> <span class="bp">*</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `n * d &lt; 0`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">fmod_add_fdiv</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span> <span class="c1">-- inductive hypothesis</span>
    <span class="k">calc</span> <span class="n">fmod</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span> <span class="bp">+</span> <span class="n">d</span> <span class="bp">*</span> <span class="o">(</span><span class="n">fdiv</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span> <span class="bp">-</span> <span class="mi">1</span><span class="o">)</span>
        <span class="bp">=</span> <span class="o">(</span><span class="n">fmod</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span> <span class="bp">+</span> <span class="n">d</span> <span class="bp">*</span> <span class="n">fdiv</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span><span class="o">)</span> <span class="bp">-</span> <span class="n">d</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="n">d</span><span class="o">)</span> <span class="bp">-</span> <span class="n">d</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">IH</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `0 &lt; d * (n - d)`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">fmod_add_fdiv</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span> <span class="c1">-- inductive hypothesis</span>
    <span class="k">calc</span> <span class="n">fmod</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span> <span class="bp">+</span> <span class="n">d</span> <span class="bp">*</span> <span class="o">(</span><span class="n">fdiv</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span> <span class="bp">+</span> <span class="mi">1</span><span class="o">)</span>
        <span class="bp">=</span> <span class="o">(</span><span class="n">fmod</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span> <span class="bp">+</span> <span class="n">d</span> <span class="bp">*</span> <span class="n">fdiv</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="n">d</span><span class="o">)</span> <span class="bp">+</span> <span class="n">d</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
        <span class="n">_</span> <span class="bp">=</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">addarith</span> <span class="o">[</span><span class="n">IH</span><span class="o">]</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `n = d`</span>
    <span class="k">calc</span> <span class="mi">0</span> <span class="bp">+</span> <span class="n">d</span> <span class="bp">*</span> <span class="mi">1</span> <span class="bp">=</span> <span class="n">d</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">n</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">h3</span><span class="o">]</span>
  <span class="bp">&#183;</span> <span class="c1">-- last case</span>
    <span class="n">ring</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">n</span> <span class="n">d</span> <span class="bp">=&gt;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">n</span> <span class="bp">-</span> <span class="n">d</span>
</pre></div>
</div>
</section>
<section id="mod-nonneg">
<span id="id32"></span><h3><span class="section-number">6.6.3. </span>Example<a class="headerlink" href="#mod-nonneg" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-theorem admonition">
<p class="admonition-title">Theorem</p>
<p>For any integers <span class="math notranslate nohighlight">\(n\)</span> and <span class="math notranslate nohighlight">\(d\)</span>, with <span class="math notranslate nohighlight">\(d\)</span> positive, we have that
<span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)\)</span> is nonnegative.</p>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by strong induction relative to the expression <span class="math notranslate nohighlight">\(2n - d\)</span>.  Suppose that for all
integers <span class="math notranslate nohighlight">\(m\)</span> and <span class="math notranslate nohighlight">\(c\)</span> with <span class="math notranslate nohighlight">\(c\)</span> positive and <span class="math notranslate nohighlight">\(|2m - c|&lt;|2n-d|\)</span>, it is true
that <span class="math notranslate nohighlight">\(\operatorname{mod}(m, c)\)</span> is nonnegative.</p>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(nd&lt;0\)</span>): Then by the inductive hypothesis
<span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)=\operatorname{mod}(n + d, d)\geq 0\)</span>.</p>
<p><strong>Case 2</strong> (<span class="math notranslate nohighlight">\(0&lt;d(n-d)\)</span>): Then by the inductive hypothesis
<span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)=\operatorname{mod}(n - d, d)\geq 0\)</span>.</p>
<p><strong>Case 3</strong> (<span class="math notranslate nohighlight">\(n=d\)</span>): Then <span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)= 0\)</span>, so
<span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)\geq 0\)</span>.</p>
<p><strong>Case 4</strong> (<span class="math notranslate nohighlight">\(0\le nd\le d^2\)</span> and <span class="math notranslate nohighlight">\(n\ne d\)</span>): Then since
<span class="math notranslate nohighlight">\(0\le nd\)</span> and by hypothesis <span class="math notranslate nohighlight">\(0&lt;d\)</span>, we have that
<span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)=n\geq 0\)</span>.</p>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">fmod_nonneg_of_pos</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">{</span><span class="n">d</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">}</span> <span class="o">(</span><span class="n">hd</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">d</span><span class="o">)</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="n">fmod</span> <span class="n">n</span> <span class="n">d</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">rw</span> <span class="o">[</span><span class="n">fmod</span><span class="o">]</span>
  <span class="n">split_ifs</span> <span class="k">with</span> <span class="n">h1</span> <span class="n">h2</span> <span class="n">h3</span> <span class="bp">&lt;;&gt;</span> <span class="n">push_neg</span> <span class="n">at</span> <span class="bp">*</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `n * d &lt; 0`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">fmod_nonneg_of_pos</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="n">d</span><span class="o">)</span> <span class="n">hd</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">apply</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `0 &lt; d * (n - d)`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">fmod_nonneg_of_pos</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="n">hd</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">apply</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `n = d`</span>
    <span class="n">extra</span>
  <span class="bp">&#183;</span> <span class="c1">-- last case</span>
    <span class="n">cancel</span> <span class="n">d</span> <span class="n">at</span> <span class="n">h1</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">n</span> <span class="n">d</span> <span class="n">hd</span> <span class="bp">=&gt;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">n</span> <span class="bp">-</span> <span class="n">d</span>
</pre></div>
</div>
</section>
<section id="mod-lt">
<span id="id33"></span><h3><span class="section-number">6.6.4. </span>Example<a class="headerlink" href="#mod-lt" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-theorem admonition">
<p class="admonition-title">Theorem</p>
<p>For any integers <span class="math notranslate nohighlight">\(n\)</span> and <span class="math notranslate nohighlight">\(d\)</span>, with <span class="math notranslate nohighlight">\(d\)</span> positive, we have that
<span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)&lt;d\)</span>.</p>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by strong induction relative to the expression <span class="math notranslate nohighlight">\(2n - d\)</span>.  Suppose that for all
integers <span class="math notranslate nohighlight">\(m\)</span> and <span class="math notranslate nohighlight">\(c\)</span> with <span class="math notranslate nohighlight">\(c\)</span> positive and <span class="math notranslate nohighlight">\(|2m - c|&lt;|2n-d|\)</span>, it is true
that <span class="math notranslate nohighlight">\(\operatorname{mod}(m, c)&lt;c\)</span>.</p>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(nd&lt;0\)</span>): Then by the inductive hypothesis
<span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)=\operatorname{mod}(n + d, d)&lt;d\)</span>.</p>
<p><strong>Case 2</strong> (<span class="math notranslate nohighlight">\(0&lt;d(n-d)\)</span>): Then by the inductive hypothesis
<span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)=\operatorname{mod}(n - d, d)&lt;d\)</span>.</p>
<p><strong>Case 3</strong> (<span class="math notranslate nohighlight">\(n=d\)</span>): Then <span class="math notranslate nohighlight">\(\operatorname{mod}(n, d)= 0&lt;d\)</span> by hypothesis.</p>
<p><strong>Case 4</strong> (<span class="math notranslate nohighlight">\(0\le nd\le d^2\)</span> and <span class="math notranslate nohighlight">\(n\ne d\)</span>): We have that <span class="math notranslate nohighlight">\(n-d\le 0\)</span>, since
<span class="math notranslate nohighlight">\(d(n-d)\le 0\)</span> and by hypothesis <span class="math notranslate nohighlight">\(0&lt;d\)</span>. Therefore <span class="math notranslate nohighlight">\(n\le d\)</span>. Also, by hypothesis,
<span class="math notranslate nohighlight">\(n\ne d\)</span>.  Combining these, we have that <span class="math notranslate nohighlight">\(n&lt; d\)</span>.</p>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">fmod_lt_of_pos</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">{</span><span class="n">d</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">}</span> <span class="o">(</span><span class="n">hd</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">d</span><span class="o">)</span> <span class="o">:</span> <span class="n">fmod</span> <span class="n">n</span> <span class="n">d</span> <span class="bp">&lt;</span> <span class="n">d</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">rw</span> <span class="o">[</span><span class="n">fmod</span><span class="o">]</span>
  <span class="n">split_ifs</span> <span class="k">with</span> <span class="n">h1</span> <span class="n">h2</span> <span class="n">h3</span> <span class="bp">&lt;;&gt;</span> <span class="n">push_neg</span> <span class="n">at</span> <span class="bp">*</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `n * d &lt; 0`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">fmod_lt_of_pos</span> <span class="o">(</span><span class="n">n</span> <span class="bp">+</span> <span class="n">d</span><span class="o">)</span> <span class="n">hd</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">apply</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `0 &lt; d * (n - d)`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">fmod_lt_of_pos</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="n">hd</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">apply</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `n = d`</span>
    <span class="n">apply</span> <span class="n">hd</span>
  <span class="bp">&#183;</span> <span class="c1">-- last case</span>
    <span class="k">have</span> <span class="n">h4</span> <span class="o">:=</span>
    <span class="k">calc</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="bp">-</span> <span class="n">d</span> <span class="bp">*</span> <span class="o">(</span><span class="n">n</span> <span class="bp">-</span> <span class="n">d</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">addarith</span> <span class="o">[</span><span class="n">h2</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">d</span> <span class="bp">*</span> <span class="o">(</span><span class="n">d</span> <span class="bp">-</span> <span class="n">n</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
    <span class="n">cancel</span> <span class="n">d</span> <span class="n">at</span> <span class="n">h4</span>
    <span class="n">apply</span> <span class="n">lt_of_le_of_ne</span>
    <span class="bp">&#183;</span> <span class="n">addarith</span> <span class="o">[</span><span class="n">h4</span><span class="o">]</span>
    <span class="bp">&#183;</span> <span class="n">apply</span> <span class="n">h3</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">n</span> <span class="n">d</span> <span class="n">hd</span> <span class="bp">=&gt;</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">n</span> <span class="bp">-</span> <span class="n">d</span>
</pre></div>
</div>
</section>
<section id="weak-division-algorithm">
<span id="id34"></span><h3><span class="section-number">6.6.5. </span>Example<a class="headerlink" href="#weak-division-algorithm" title="Permalink to this headline">&#61633;</a></h3>
<p>Putting this all together, we can prove the following theorem.  This theorem justifies the tactic
<code class="docutils literal notranslate"><span class="pre">mod_cases</span></code>, which we have been using since <a class="reference internal" href="03_Parity_and_Divisibility.html#first-mod-cases"><span class="std std-numref">Example 3.4.4</span></a>: we can list out
just finitely many possibilities for an integer <span class="math notranslate nohighlight">\(a\)</span>, considered modulo a positive integer
<span class="math notranslate nohighlight">\(b\)</span>.</p>
<div class="admonition-theorem admonition">
<p class="admonition-title">Theorem</p>
<p>Let <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> be integers, with <span class="math notranslate nohighlight">\(b\)</span> positive.   There exists an
integer <span class="math notranslate nohighlight">\(r\)</span>, with <span class="math notranslate nohighlight">\(0 \le r &lt; b\)</span>, such that <span class="math notranslate nohighlight">\(a \equiv r\mod b\)</span>.</p>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We will show that the integer <span class="math notranslate nohighlight">\(\operatorname{mod}(a,b)\)</span> has this property.  Indeed, by
<a class="reference internal" href="#mod-nonneg"><span class="std std-numref">Example 6.6.3</span></a> and <a class="reference internal" href="#mod-lt"><span class="std std-numref">Example 6.6.4</span></a>,
<span class="math notranslate nohighlight">\(0 \le \operatorname{mod}(a,b) &lt; b\)</span>, and by <a class="reference internal" href="#mod-add-div"><span class="std std-numref">Example 6.6.2</span></a>,</p>
<div class="math notranslate nohighlight">
\[\operatorname{mod}(a, b) + b \cdot \operatorname{div}(a, b) = a,\]</div>
<p>so</p>
<div class="math notranslate nohighlight">
\[a-\operatorname{mod}(a, b)=b \cdot \operatorname{div}(a, b),\]</div>
<p>so</p>
<div class="math notranslate nohighlight">
\[a \equiv \operatorname{mod}(a, b)\mod b.\]</div>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">example</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">(</span><span class="n">h</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">b</span><span class="o">)</span> <span class="o">:</span> <span class="bp">&#8707;</span> <span class="n">r</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">,</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="n">r</span> <span class="bp">&#8743;</span> <span class="n">r</span> <span class="bp">&lt;</span> <span class="n">b</span> <span class="bp">&#8743;</span> <span class="n">a</span> <span class="bp">&#8801;</span> <span class="n">r</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="n">b</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">use</span> <span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span>
  <span class="n">constructor</span>
  <span class="bp">&#183;</span> <span class="n">apply</span> <span class="n">fmod_nonneg_of_pos</span> <span class="n">a</span> <span class="n">h</span>
  <span class="n">constructor</span>
  <span class="bp">&#183;</span> <span class="n">apply</span> <span class="n">fmod_lt_of_pos</span> <span class="n">a</span> <span class="n">h</span>
  <span class="bp">&#183;</span> <span class="n">use</span> <span class="n">fdiv</span> <span class="n">a</span> <span class="n">b</span>
    <span class="k">have</span> <span class="n">Hab</span> <span class="o">:</span> <span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">+</span> <span class="n">b</span> <span class="bp">*</span> <span class="n">fdiv</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=</span> <span class="n">a</span> <span class="o">:=</span> <span class="n">fmod_add_fdiv</span> <span class="n">a</span> <span class="n">b</span>
    <span class="n">addarith</span> <span class="o">[</span><span class="n">Hab</span><span class="o">]</span>
</pre></div>
</div>
</section>
<section id="id35">
<h3><span class="section-number">6.6.6. </span>Exercises<a class="headerlink" href="#id35" title="Permalink to this headline">&#61633;</a></h3>
<ol class="arabic">
<li><p>Prove the analogue of  <a class="reference internal" href="#mod-lt"><span class="std std-numref">Example 6.6.4</span></a> for <span class="math notranslate nohighlight">\(d\)</span> negative:
For any integers <span class="math notranslate nohighlight">\(n\)</span> and <span class="math notranslate nohighlight">\(d\)</span>, with <span class="math notranslate nohighlight">\(d\)</span> negative, we have that
<span class="math notranslate nohighlight">\(d&lt;\operatorname{mod}(n, d)\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">lt_fmod_of_neg</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">{</span><span class="n">d</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">}</span> <span class="o">(</span><span class="n">hd</span> <span class="o">:</span> <span class="n">d</span> <span class="bp">&lt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">:</span> <span class="n">d</span> <span class="bp">&lt;</span> <span class="n">fmod</span> <span class="n">n</span> <span class="n">d</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Consider the function <span class="math notranslate nohighlight">\(T\)</span> defined recursively on the integers by,</p>
<div class="math notranslate nohighlight">
\[\begin{split}T(n)=
\begin{cases}
T(1-n)+2n-1,&amp;0&lt; n\\
T(-n),&amp;n&lt; 0\\
0&amp;n=0.
\end{cases}\end{split}\]</div>
<p>This recursive definition is well-founded, since its self-references are strictly decreasing in
<span class="math notranslate nohighlight">\(|3n-1|\)</span>.</p>
<p>Prove that for all integers <span class="math notranslate nohighlight">\(n\)</span>, <span class="math notranslate nohighlight">\(T(n)=n^2\)</span>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">T</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">&#8484;</span> <span class="o">:=</span>
  <span class="k">if</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">n</span> <span class="k">then</span>
    <span class="n">T</span> <span class="o">(</span><span class="mi">1</span> <span class="bp">-</span> <span class="n">n</span><span class="o">)</span> <span class="bp">+</span> <span class="mi">2</span> <span class="bp">*</span> <span class="n">n</span> <span class="bp">-</span> <span class="mi">1</span>
  <span class="k">else</span> <span class="k">if</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="bp">-</span> <span class="n">n</span> <span class="k">then</span>
    <span class="n">T</span> <span class="o">(</span><span class="bp">-</span><span class="n">n</span><span class="o">)</span>
  <span class="k">else</span>
    <span class="mi">0</span>
<span class="n">termination_by</span> <span class="n">T</span> <span class="n">n</span> <span class="bp">=&gt;</span> <span class="mi">3</span> <span class="bp">*</span> <span class="n">n</span> <span class="bp">-</span> <span class="mi">1</span>

<span class="kd">theorem</span> <span class="n">T_eq</span> <span class="o">(</span><span class="n">n</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">T</span> <span class="n">n</span> <span class="bp">=</span> <span class="n">n</span> <span class="bp">^</span> <span class="mi">2</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
<li><p>Let <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> be integers, with <span class="math notranslate nohighlight">\(b\)</span> positive.  Prove that there exists a
unique integer <span class="math notranslate nohighlight">\(r\)</span> in the range <span class="math notranslate nohighlight">\(0 \le r &lt; b\)</span>, such that <span class="math notranslate nohighlight">\(a\equiv r\mod b\)</span>.</p>
<p>This theorem is an upgrade to <a class="reference internal" href="#weak-division-algorithm"><span class="std std-numref">Example 6.6.5</span></a>, stating uniqueness
as well as existence.  We stated it without proof in <a class="reference internal" href="04_Proofs_with_Structure_II.html#division-algorithm"><span class="std std-numref">Example 4.3.4</span></a>
(with the Lean name <code class="docutils literal notranslate"><span class="pre">Int.existsUnique_modEq_lt</span></code>) and have been implicitly using it whenever we
deduce a contradiction involving &#8220;obvious non-congruences&#8221;, such as in
<a class="reference internal" href="04_Proofs_with_Structure_II.html#mod-contradictory"><span class="std std-numref">Example 4.4.3</span></a>.</p>
<p>Suggested approach:  Write the following as a stand-alone theorem <code class="docutils literal notranslate"><span class="pre">uniqueness</span></code> and prove it,
somewhat along the lines of the special case proved in
<a class="reference internal" href="04_Proofs_with_Structure_II.html#division-algorithm"><span class="std std-numref">Example 4.3.4</span></a>:</p>
<blockquote>
<div><p>Let <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> be integers, with <span class="math notranslate nohighlight">\(b\)</span> positive. Let <span class="math notranslate nohighlight">\(r\)</span> and <span class="math notranslate nohighlight">\(s\)</span>
be integers, both in the range <span class="math notranslate nohighlight">\(0 \le r &lt; b\)</span>, <span class="math notranslate nohighlight">\(0 \le s &lt; b\)</span> and both congruent to
<span class="math notranslate nohighlight">\(a\)</span> modulo <span class="math notranslate nohighlight">\(b\)</span>.  Show that they are equal.</p>
</div></blockquote>
<p>Then put all the pieces together, combining that with the argument from
<a class="reference internal" href="#weak-division-algorithm"><span class="std std-numref">Example 6.6.5</span></a>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">uniqueness</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">(</span><span class="n">h</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">b</span><span class="o">)</span> <span class="o">{</span><span class="n">r</span> <span class="n">s</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">}</span>
    <span class="o">(</span><span class="n">hr</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="n">r</span> <span class="bp">&#8743;</span> <span class="n">r</span> <span class="bp">&lt;</span> <span class="n">b</span> <span class="bp">&#8743;</span> <span class="n">a</span> <span class="bp">&#8801;</span> <span class="n">r</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="n">b</span><span class="o">])</span>
    <span class="o">(</span><span class="n">hs</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="n">s</span> <span class="bp">&#8743;</span> <span class="n">s</span> <span class="bp">&lt;</span> <span class="n">b</span> <span class="bp">&#8743;</span> <span class="n">a</span> <span class="bp">&#8801;</span> <span class="n">s</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="n">b</span><span class="o">])</span> <span class="o">:</span> <span class="n">r</span> <span class="bp">=</span> <span class="n">s</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>

<span class="kd">example</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">(</span><span class="n">h</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">b</span><span class="o">)</span> <span class="o">:</span> <span class="bp">&#8707;!</span> <span class="n">r</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">,</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="n">r</span> <span class="bp">&#8743;</span> <span class="n">r</span> <span class="bp">&lt;</span> <span class="n">b</span> <span class="bp">&#8743;</span> <span class="n">a</span> <span class="bp">&#8801;</span> <span class="n">r</span> <span class="o">[</span><span class="n">ZMOD</span> <span class="n">b</span><span class="o">]</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
</ol>
</section>
</section>
<section id="the-euclidean-algorithm">
<span id="euclidean-algorithm"></span><h2><span class="section-number">6.7. </span>The Euclidean algorithm<a class="headerlink" href="#the-euclidean-algorithm" title="Permalink to this headline">&#61633;</a></h2>
<section id="euclidean-algorithm-def">
<span id="id36"></span><h3><span class="section-number">6.7.1. </span>Definition<a class="headerlink" href="#euclidean-algorithm-def" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-definition admonition">
<p class="admonition-title">Definition</p>
<p>The function <span class="math notranslate nohighlight">\(\operatorname{gcd}\)</span>  of two integers is defined recursively as follows:</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}\operatorname{gcd}(a,b)=
\begin{cases}
  \operatorname{gcd}(b,\operatorname{mod}(a,b)) &amp; 0&lt; b\\
  \operatorname{gcd}(b,\operatorname{mod}(a,-b)) &amp; b&lt; 0\\
  a &amp; b=0\text{ and }0\le a\\
  -a &amp; b=0\text{ and }a&lt;0.
\end{cases}\end{split}\]</div>
</div></blockquote>
</div>
<p>Let&#8217;s practice evaluating the <span class="math notranslate nohighlight">\(\operatorname{gcd}\)</span> function.  We calculate
<span class="math notranslate nohighlight">\(\operatorname{gcd}(-21,15)\)</span>:</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}\operatorname{mod}(-21,15)&amp;=9
&amp;
&amp;\qquad
&amp;
\operatorname{gcd}(-21,15)&amp;=\operatorname{gcd}(15,9)\\\operatorname{mod}(15,9)&amp;=6
&amp;
&amp;\qquad
&amp;
&amp;=\operatorname{gcd}(9,6)\\\operatorname{mod}(9,6)&amp;=3
&amp;
&amp;\qquad
&amp;
&amp;=\operatorname{gcd}(6,3)\\\operatorname{mod}(6,3)&amp;=0
&amp;
&amp;\qquad
&amp;
&amp;=\operatorname{gcd}(3,0)\\&amp;
&amp;
&amp;\qquad
&amp;
&amp;=3.\end{aligned}\end{align} \]</div>
<p>(Remember that <span class="math notranslate nohighlight">\(\operatorname{mod}(a,b)\)</span> is the &#8220;remainder&#8221; function, defined in
<a class="reference internal" href="#constructive-division-algorithm-def"><span class="std std-numref">Definition 6.6.1</span></a>.)</p>
<p>As in <a class="reference internal" href="#pascal-def"><span class="std std-numref">Definition 6.5.1</span></a> and
<a class="reference internal" href="#constructive-division-algorithm-def"><span class="std std-numref">Definition 6.6.1</span></a>, we
need to justify that this recursive definition is <em>well-founded</em>, that is that the process always
terminates.  And as in those sections, we do this by providing an expression which becomes strictly
smaller in absolute value as the process goes on.  Here that expression is <span class="math notranslate nohighlight">\(b\)</span>, the second of
the two numbers (note that in the example we have <span class="math notranslate nohighlight">\(b\)</span> being successively 15, 9, 6, 3, 0, which
indeed is decreasing.)</p>
<p>This leads to the following attempt at a Lean definition for  <span class="math notranslate nohighlight">\(\operatorname{gcd}\)</span>, with the
clause <code class="docutils literal notranslate"><span class="pre">termination_by</span> <span class="pre">_</span> <span class="pre">a</span> <span class="pre">b</span> <span class="pre">=&gt;</span> <span class="pre">b</span></code> indicating that <span class="math notranslate nohighlight">\(b\)</span> is the size expression for the
well-foundedness.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">def</span> <span class="n">gcd</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">&#8484;</span> <span class="o">:=</span>
  <span class="k">if</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">b</span> <span class="k">then</span>
    <span class="n">gcd</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span>
  <span class="k">else</span> <span class="k">if</span> <span class="n">b</span> <span class="bp">&lt;</span> <span class="mi">0</span> <span class="k">then</span>
    <span class="n">gcd</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span>
  <span class="k">else</span> <span class="k">if</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="n">a</span> <span class="k">then</span>
    <span class="n">a</span>
  <span class="k">else</span>
    <span class="bp">-</span><span class="n">a</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">b</span>
</pre></div>
</div>
<p>But unlike in <a class="reference internal" href="#pascal"><span class="std std-numref">Section 6.5</span></a> and
<a class="reference internal" href="#constructive-division-algorithm"><span class="std std-numref">Section 6.6</span></a>, the definition is not yet complete: the fact
that <span class="math notranslate nohighlight">\(b\)</span> is decreasing along the recursion is &#8220;non-obvious enough&#8221; that it requires an
explicit proof.</p>
<div class="admonition-proposition admonition">
<p class="admonition-title">Proposition</p>
<p>The recursive definition <span class="math notranslate nohighlight">\(\operatorname{gcd}\)</span> is well-founded.</p>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>There are two things to check:</p>
<ol class="arabic">
<li><p><span class="math notranslate nohighlight">\(\underline{\text{If }0&lt;b\text{ then }-b&lt;\operatorname{mod}(a,b)&lt;b}\)</span>:
By <a class="reference internal" href="#mod-nonneg"><span class="std std-numref">Example 6.6.3</span></a> and <a class="reference internal" href="#mod-lt"><span class="std std-numref">Example 6.6.4</span></a>,
<span class="math notranslate nohighlight">\(0\le \operatorname{mod}(a,b)&lt;b\)</span>, which establishes the upper bound immediately and the
lower bound since</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}-b&amp;&lt;0\\
&amp;\le \operatorname{mod}(a,b).\end{split}\]</div>
</div></blockquote>
</li>
<li><p><span class="math notranslate nohighlight">\(\underline{\text{If }b&lt;0\text{ then }b&lt;\operatorname{mod}(a,-b)&lt;-b}\)</span>:
We have that <span class="math notranslate nohighlight">\(0&lt;-b\)</span>,
so by <a class="reference internal" href="#mod-nonneg"><span class="std std-numref">Example 6.6.3</span></a> and <a class="reference internal" href="#mod-lt"><span class="std std-numref">Example 6.6.4</span></a>,
<span class="math notranslate nohighlight">\(0\le \operatorname{mod}(a,-b)&lt;-b\)</span>, which establishes the upper bound immediately and
the lower bound since</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}b&amp;&lt;0\\
&amp;\le \operatorname{mod}(a,-b).\end{split}\]</div>
</div></blockquote>
</li>
</ol>
</div>
<p>In Lean, we state and prove these facts separately, tagging them with the attribute
<code class="docutils literal notranslate"><span class="pre">&#64;[decreasing]</span></code>, which lets the subsequent definition <code class="docutils literal notranslate"><span class="pre">gcd</span></code> call on them for the
well-foundedness.  Check that if they are omitted then the definition gives an error.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">@[decreasing]</span> <span class="kd">theorem</span> <span class="n">lower_bound_fmod1</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">(</span><span class="n">h1</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">b</span><span class="o">)</span> <span class="o">:</span> <span class="bp">-</span><span class="n">b</span> <span class="bp">&lt;</span> <span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="k">have</span> <span class="n">H</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span>
  <span class="bp">&#183;</span> <span class="n">apply</span> <span class="n">fmod_nonneg_of_pos</span>
    <span class="n">apply</span> <span class="n">h1</span>
  <span class="k">calc</span> <span class="bp">-</span><span class="n">b</span> <span class="bp">&lt;</span> <span class="mi">0</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">addarith</span> <span class="o">[</span><span class="n">h1</span><span class="o">]</span>
    <span class="n">_</span> <span class="bp">&#8804;</span> <span class="n">_</span> <span class="o">:=</span> <span class="n">H</span>

<span class="kd">@[decreasing]</span> <span class="kd">theorem</span> <span class="n">lower_bound_fmod2</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">(</span><span class="n">h1</span> <span class="o">:</span> <span class="n">b</span> <span class="bp">&lt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">:</span> <span class="n">b</span> <span class="bp">&lt;</span> <span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="k">have</span> <span class="n">H</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span>
  <span class="bp">&#183;</span> <span class="n">apply</span> <span class="n">fmod_nonneg_of_pos</span>
    <span class="n">addarith</span> <span class="o">[</span><span class="n">h1</span><span class="o">]</span>
  <span class="k">have</span> <span class="n">h2</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="bp">-</span><span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">addarith</span> <span class="o">[</span><span class="n">h1</span><span class="o">]</span>
  <span class="k">calc</span> <span class="n">b</span> <span class="bp">&lt;</span> <span class="mi">0</span> <span class="o">:=</span> <span class="n">h1</span>
    <span class="n">_</span> <span class="bp">&#8804;</span> <span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span> <span class="o">:=</span> <span class="n">H</span>

<span class="kd">@[decreasing]</span> <span class="kd">theorem</span> <span class="n">upper_bound_fmod2</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">(</span><span class="n">h1</span> <span class="o">:</span> <span class="n">b</span> <span class="bp">&lt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">:</span> <span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span> <span class="bp">&lt;</span> <span class="bp">-</span><span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">apply</span> <span class="n">fmod_lt_of_pos</span>
  <span class="n">addarith</span> <span class="o">[</span><span class="n">h1</span><span class="o">]</span>

<span class="kd">@[decreasing]</span> <span class="kd">theorem</span> <span class="n">upper_bound_fmod1</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">(</span><span class="n">h1</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">b</span><span class="o">)</span> <span class="o">:</span> <span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">&lt;</span> <span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">apply</span> <span class="n">fmod_lt_of_pos</span>
  <span class="n">apply</span> <span class="n">h1</span>
</pre></div>
</div>
<p>After this the Lean definition <code class="docutils literal notranslate"><span class="pre">gcd</span></code> goes through successfully. Sanity check:  does the Lean
definition agree with our calculation above for <span class="math notranslate nohighlight">\(\operatorname{gcd}(-21,15)\)</span>?</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="k">#eval</span> <span class="n">gcd</span> <span class="o">(</span><span class="bp">-</span><span class="mi">21</span><span class="o">)</span> <span class="mi">15</span> <span class="c1">-- infoview displays `3`</span>
</pre></div>
</div>
</section>
<section id="gcd-nonneg">
<span id="id37"></span><h3><span class="section-number">6.7.2. </span>Example<a class="headerlink" href="#gcd-nonneg" title="Permalink to this headline">&#61633;</a></h3>
<p>Every fact about <span class="math notranslate nohighlight">\(\operatorname{gcd}\)</span> will be proved by strong induction, using the same
well-foundedness justification.</p>
<div class="admonition-proposition admonition">
<p class="admonition-title">Proposition</p>
<p>For all integers <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>, the integer <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span> is
nonnegative.</p>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by strong induction on <span class="math notranslate nohighlight">\(b\)</span>.   Suppose that for all
integers <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> with <span class="math notranslate nohighlight">\(|y|&lt;|b|\)</span>, it is true that
<span class="math notranslate nohighlight">\(0 \le  \operatorname{gcd}(x, y)\)</span>.</p>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(0&lt;b\)</span>): Then <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span> is equal to
<span class="math notranslate nohighlight">\(\operatorname{gcd}(b,\operatorname{mod}(a,b))\)</span> which by the inductive hypothesis is
nonnegative.</p>
<p><strong>Case 2</strong> (<span class="math notranslate nohighlight">\(b&lt;0\)</span>): Then <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span> is equal to
<span class="math notranslate nohighlight">\(\operatorname{gcd}(b,\operatorname{mod}(a,-b))\)</span> which by the inductive hypothesis is
nonnegative.</p>
<p><strong>Case 3</strong> (<span class="math notranslate nohighlight">\(b=0\)</span>, <span class="math notranslate nohighlight">\(0\le a\)</span>): Then we have that
<span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)=a\geq 0\)</span>.</p>
<p><strong>Case 4</strong> (<span class="math notranslate nohighlight">\(b=0\)</span>, <span class="math notranslate nohighlight">\(a&lt;0\)</span>): Then we have that <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)=-a\geq 0\)</span>.</p>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">gcd_nonneg</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">rw</span> <span class="o">[</span><span class="n">gcd</span><span class="o">]</span>
  <span class="n">split_ifs</span> <span class="k">with</span> <span class="n">h1</span> <span class="n">h2</span> <span class="n">ha</span> <span class="bp">&lt;;&gt;</span> <span class="n">push_neg</span> <span class="n">at</span> <span class="bp">*</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `0 &lt; b`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">gcd_nonneg</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">apply</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b &lt; 0`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">gcd_nonneg</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">apply</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `0 &#8804; a`</span>
    <span class="n">apply</span> <span class="n">ha</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `a &lt; 0`</span>
    <span class="n">addarith</span> <span class="o">[</span><span class="n">ha</span><span class="o">]</span>
<span class="n">termination_by</span> <span class="n">_</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">b</span>
</pre></div>
</div>
</section>
<section id="common-divisor">
<span id="id38"></span><h3><span class="section-number">6.7.3. </span>Example<a class="headerlink" href="#common-divisor" title="Permalink to this headline">&#61633;</a></h3>
<div class="admonition-proposition admonition">
<p class="admonition-title">Proposition</p>
<p>For all integers <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>, the integer <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span> is a factor
of both <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>.</p>
</div>
<p>That is, <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span> is a <em>common divisor</em> of <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>.  We will
prove later (see the exercises) that in fact it is the <em>greatest common divisor</em> of <span class="math notranslate nohighlight">\(a\)</span> and
<span class="math notranslate nohighlight">\(b\)</span>, hence the acronym GCD.</p>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by strong induction on <span class="math notranslate nohighlight">\(b\)</span>.   Suppose that for all
integers <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> with <span class="math notranslate nohighlight">\(|y|&lt;|b|\)</span>, it is true that
<span class="math notranslate nohighlight">\(0 \le  \operatorname{gcd}(x, y)\)</span>.</p>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(0&lt;b\)</span>): Let <span class="math notranslate nohighlight">\(q=\operatorname{div}(a,b)\)</span> and let
<span class="math notranslate nohighlight">\(r=\operatorname{mod}(a,b)\)</span>, so that <span class="math notranslate nohighlight">\(a=r+bq\)</span> (by <a class="reference internal" href="#mod-add-div"><span class="std std-numref">Example 6.6.2</span></a>).</p>
<p>Then by the recursive definition <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span> is equal to
<span class="math notranslate nohighlight">\(\operatorname{gcd}(b,r)\)</span>, and by the inductive hypothesis this
divides both <span class="math notranslate nohighlight">\(b\)</span> and <span class="math notranslate nohighlight">\(r\)</span>.  We need to show it divides <span class="math notranslate nohighlight">\(b\)</span>
(which is immediate) and <span class="math notranslate nohighlight">\(a\)</span>, which we now turn to.</p>
<p>Since <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\mid b\)</span>, there exists an integer <span class="math notranslate nohighlight">\(k\)</span> such that
<span class="math notranslate nohighlight">\(b = \operatorname{gcd}(a,b)k\)</span>, and since <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\mid r\)</span>,
there exists an integer <span class="math notranslate nohighlight">\(l\)</span> such that <span class="math notranslate nohighlight">\(r = \operatorname{gcd}(a,b)l\)</span>.  We now have</p>
<div class="math notranslate nohighlight">
\[\begin{split}a&amp;=r+bq\\
&amp;=\operatorname{gcd}(a,b)l+(\operatorname{gcd}(a,b)k)q\\
&amp;=\operatorname{gcd}(a,b)\cdot (l+kq),\end{split}\]</div>
<p>so <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\mid a\)</span>.</p>
<p><strong>Case 2</strong> (<span class="math notranslate nohighlight">\(b&lt;0\)</span>): Let <span class="math notranslate nohighlight">\(q=\operatorname{div}(a,-b)\)</span> and let
<span class="math notranslate nohighlight">\(r=\operatorname{mod}(a,-b)\)</span>, so that <span class="math notranslate nohighlight">\(a=r+(-b)q\)</span> (by
<a class="reference internal" href="#mod-add-div"><span class="std std-numref">Example 6.6.2</span></a>).</p>
<p>Then by the recursive definition <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span> is equal to
<span class="math notranslate nohighlight">\(\operatorname{gcd}(b,r)\)</span>, and by the inductive hypothesis this
divides both <span class="math notranslate nohighlight">\(b\)</span> and <span class="math notranslate nohighlight">\(r\)</span>.  We need to show it divides <span class="math notranslate nohighlight">\(b\)</span>
(which is immediate) and <span class="math notranslate nohighlight">\(a\)</span>, which we now turn to.</p>
<p>Since <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\mid b\)</span>, there exists an integer <span class="math notranslate nohighlight">\(k\)</span> such that
<span class="math notranslate nohighlight">\(b = \operatorname{gcd}(a,b)k\)</span>, and since <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\mid r\)</span>,
there exists an integer <span class="math notranslate nohighlight">\(l\)</span> such that <span class="math notranslate nohighlight">\(r = \operatorname{gcd}(a,b)l\)</span>.  We now have</p>
<div class="math notranslate nohighlight">
\[\begin{split}a&amp;=r+(-b)q\\
&amp;=\operatorname{gcd}(a,b)l+(-\operatorname{gcd}(a,b)k)q\\
&amp;=\operatorname{gcd}(a,b)\cdot (l-kq),\end{split}\]</div>
<p>so <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\mid a\)</span>.</p>
<p><strong>Case 3</strong> (<span class="math notranslate nohighlight">\(b=0\)</span>, <span class="math notranslate nohighlight">\(0\le a\)</span>): Then we have that
<span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)=a\)</span>, which is a factor of <span class="math notranslate nohighlight">\(a\)</span> since <span class="math notranslate nohighlight">\(a\cdot 1=a\)</span> and
<span class="math notranslate nohighlight">\(b\)</span> since</p>
<div class="math notranslate nohighlight">
\[\begin{split}b &amp;= 0\\
&amp;=0\cdot a.\end{split}\]</div>
<p><strong>Case 4</strong> (<span class="math notranslate nohighlight">\(b=0\)</span>, <span class="math notranslate nohighlight">\(a&lt;0\)</span>): Then we have that <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)=-a\)</span>, which
is a factor of <span class="math notranslate nohighlight">\(a\)</span> since <span class="math notranslate nohighlight">\(-a\cdot -1=a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> since</p>
<div class="math notranslate nohighlight">
\[\begin{split}b &amp;= 0\\
&amp;=0\cdot -a.\end{split}\]</div>
</div>
<p>It would be possible to set up this proof in Lean with the structure of an &#8220;and&#8221; goal, like this:
(The <code class="docutils literal notranslate"><span class="pre">_</span></code> are placeholders just to show the basic structure.)</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">gcd_dvd</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">&#8739;</span> <span class="n">b</span> <span class="bp">&#8743;</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">&#8739;</span> <span class="n">a</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">rw</span> <span class="o">[</span><span class="n">gcd</span><span class="o">]</span>
  <span class="n">split_ifs</span> <span class="k">with</span> <span class="n">h1</span> <span class="n">h2</span> <span class="bp">&lt;;&gt;</span> <span class="n">push_neg</span> <span class="n">at</span> <span class="bp">*</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `0 &lt; b`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:</span> <span class="n">_</span> <span class="bp">&#8743;</span> <span class="n">_</span> <span class="o">:=</span> <span class="n">gcd_dvd</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">IH_right</span><span class="o">,</span> <span class="n">IH_left</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">IH</span>
    <span class="n">constructor</span>
    <span class="bp">&#183;</span> <span class="c1">-- prove that `gcd a b &#8739; b`</span>
      <span class="gr">sorry</span>
    <span class="bp">&#183;</span> <span class="c1">-- prove that `gcd a b &#8739; a`</span>
      <span class="gr">sorry</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b &lt; 0`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:</span> <span class="n">_</span> <span class="bp">&#8743;</span> <span class="n">_</span> <span class="o">:=</span> <span class="n">gcd_dvd</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">IH_right</span><span class="o">,</span> <span class="n">IH_left</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">IH</span>
    <span class="n">constructor</span>
    <span class="bp">&#183;</span> <span class="c1">-- prove that `gcd a b &#8739; b`</span>
      <span class="gr">sorry</span>
    <span class="bp">&#183;</span> <span class="c1">-- prove that `gcd a b &#8739; a`</span>
      <span class="gr">sorry</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `0 &#8804; a`</span>
    <span class="n">constructor</span>
    <span class="bp">&#183;</span> <span class="c1">-- prove that `gcd a b &#8739; b`</span>
      <span class="gr">sorry</span>
    <span class="bp">&#183;</span> <span class="c1">-- prove that `gcd a b &#8739; a`</span>
      <span class="gr">sorry</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `a &lt; 0`</span>
    <span class="n">constructor</span>
    <span class="bp">&#183;</span> <span class="c1">-- prove that `gcd a b &#8739; b`</span>
      <span class="gr">sorry</span>
    <span class="bp">&#183;</span> <span class="c1">-- prove that `gcd a b &#8739; a`</span>
      <span class="gr">sorry</span>
<span class="n">termination_by</span> <span class="n">gcd_dvd</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">b</span>
</pre></div>
</div>
<p>But the constant switching between the <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\mid b\)</span> task and the
<span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\mid a\)</span> task is a little hard to keep track of.  A more elegant setup
features two separate lemmas, one to show <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\mid b\)</span> and one to show
<span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\mid a\)</span>, with the following structure:</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">gcd_dvd_right</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">&#8739;</span> <span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">rw</span> <span class="o">[</span><span class="n">gcd</span><span class="o">]</span>
  <span class="n">split_ifs</span> <span class="k">with</span> <span class="n">h1</span> <span class="n">h2</span> <span class="bp">&lt;;&gt;</span> <span class="n">push_neg</span> <span class="n">at</span> <span class="bp">*</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `0 &lt; b`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">gcd_dvd_left</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b &lt; 0`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">gcd_dvd_left</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="c1">-- inductive hypothesis</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `0 &#8804; a`</span>
    <span class="gr">sorry</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `a &lt; 0`</span>
    <span class="gr">sorry</span>

<span class="kd">theorem</span> <span class="n">gcd_dvd_left</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">&#8739;</span> <span class="n">a</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">rw</span> <span class="o">[</span><span class="n">gcd</span><span class="o">]</span>
  <span class="n">split_ifs</span> <span class="k">with</span> <span class="n">h1</span> <span class="n">h2</span> <span class="bp">&lt;;&gt;</span> <span class="n">push_neg</span> <span class="n">at</span> <span class="bp">*</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `0 &lt; b`</span>
    <span class="k">have</span> <span class="n">IH1</span> <span class="o">:=</span> <span class="n">gcd_dvd_left</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
    <span class="k">have</span> <span class="n">IH2</span> <span class="o">:=</span> <span class="n">gcd_dvd_right</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
    <span class="gr">sorry</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b &lt; 0`</span>
    <span class="k">have</span> <span class="n">IH1</span> <span class="o">:=</span> <span class="n">gcd_dvd_left</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="c1">-- inductive hypothesis</span>
    <span class="k">have</span> <span class="n">IH2</span> <span class="o">:=</span> <span class="n">gcd_dvd_right</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="c1">-- inductive hypothesis</span>
    <span class="gr">sorry</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `0 &#8804; a`</span>
    <span class="gr">sorry</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `a &lt; 0`</span>
    <span class="gr">sorry</span>
</pre></div>
</div>
<p>But now the strong induction structure is complicated: the proof of <code class="docutils literal notranslate"><span class="pre">gcd_dvd_right</span></code> depends on
<code class="docutils literal notranslate"><span class="pre">gcd_dvd_left</span></code> for smaller values of <code class="docutils literal notranslate"><span class="pre">a</span></code>, <code class="docutils literal notranslate"><span class="pre">b</span></code>, and the proof of <code class="docutils literal notranslate"><span class="pre">gcd_dvd_left</span></code> depends on
<code class="docutils literal notranslate"><span class="pre">gcd_dvd_right</span></code> for smaller values of <code class="docutils literal notranslate"><span class="pre">a</span></code>, <code class="docutils literal notranslate"><span class="pre">b</span></code>.  This is called a <em>mutual induction</em> and it
has a special syntax in Lean: the two theorems are enclosed in a <code class="docutils literal notranslate"><span class="pre">mutual</span></code> block, with a joint
termination explanation at the end, like this:</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">mutual</span>

<span class="kd">theorem</span> <span class="n">gcd_dvd_right</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">&#8739;</span> <span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="bp">...</span>

<span class="kd">theorem</span> <span class="n">gcd_dvd_left</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">&#8739;</span> <span class="n">a</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="bp">...</span>

<span class="kd">end</span>
<span class="n">termination_by</span> <span class="n">gcd_dvd_right</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">b</span> <span class="bp">;</span> <span class="n">gcd_dvd_left</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">b</span>
</pre></div>
</div>
<p>Here is the full proof in Lean.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">mutual</span>
<span class="kd">theorem</span> <span class="n">gcd_dvd_right</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">&#8739;</span> <span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">rw</span> <span class="o">[</span><span class="n">gcd</span><span class="o">]</span>
  <span class="n">split_ifs</span> <span class="k">with</span> <span class="n">h1</span> <span class="n">h2</span> <span class="bp">&lt;;&gt;</span> <span class="n">push_neg</span> <span class="n">at</span> <span class="bp">*</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `0 &lt; b`</span>
    <span class="n">apply</span> <span class="n">gcd_dvd_left</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b &lt; 0`</span>
    <span class="n">apply</span> <span class="n">gcd_dvd_left</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="c1">-- inductive hypothesis</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `0 &#8804; a`</span>
    <span class="k">have</span> <span class="n">hb</span> <span class="o">:</span> <span class="n">b</span> <span class="bp">=</span> <span class="mi">0</span> <span class="o">:=</span> <span class="n">le_antisymm</span> <span class="n">h1</span> <span class="n">h2</span>
    <span class="n">use</span> <span class="mi">0</span>
    <span class="k">calc</span> <span class="n">b</span> <span class="bp">=</span> <span class="mi">0</span> <span class="o">:=</span> <span class="n">hb</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">a</span> <span class="bp">*</span> <span class="mi">0</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `a &lt; 0`</span>
    <span class="k">have</span> <span class="n">hb</span> <span class="o">:</span> <span class="n">b</span> <span class="bp">=</span> <span class="mi">0</span> <span class="o">:=</span> <span class="n">le_antisymm</span> <span class="n">h1</span> <span class="n">h2</span>
    <span class="n">use</span> <span class="mi">0</span>
    <span class="k">calc</span> <span class="n">b</span> <span class="bp">=</span> <span class="mi">0</span> <span class="o">:=</span> <span class="n">hb</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="bp">-</span><span class="n">a</span> <span class="bp">*</span> <span class="mi">0</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>

<span class="kd">theorem</span> <span class="n">gcd_dvd_left</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">&#8739;</span> <span class="n">a</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">rw</span> <span class="o">[</span><span class="n">gcd</span><span class="o">]</span>
  <span class="n">split_ifs</span> <span class="k">with</span> <span class="n">h1</span> <span class="n">h2</span> <span class="bp">&lt;;&gt;</span> <span class="n">push_neg</span> <span class="n">at</span> <span class="bp">*</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `0 &lt; b`</span>
    <span class="k">have</span> <span class="n">IH1</span> <span class="o">:=</span> <span class="n">gcd_dvd_left</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
    <span class="k">have</span> <span class="n">IH2</span> <span class="o">:=</span> <span class="n">gcd_dvd_right</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">k</span><span class="o">,</span> <span class="n">hk</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">IH1</span>
    <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">l</span><span class="o">,</span> <span class="n">hl</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">IH2</span>
    <span class="k">have</span> <span class="n">H</span> <span class="o">:</span> <span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">+</span> <span class="n">b</span> <span class="bp">*</span> <span class="n">fdiv</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=</span> <span class="n">a</span> <span class="o">:=</span> <span class="n">fmod_add_fdiv</span> <span class="n">a</span> <span class="n">b</span>
    <span class="n">set</span> <span class="n">q</span> <span class="o">:=</span> <span class="n">fdiv</span> <span class="n">a</span> <span class="n">b</span>
    <span class="n">set</span> <span class="n">r</span> <span class="o">:=</span> <span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span>
    <span class="n">use</span> <span class="n">l</span> <span class="bp">+</span> <span class="n">k</span> <span class="bp">*</span> <span class="n">q</span>
    <span class="k">calc</span> <span class="n">a</span> <span class="bp">=</span> <span class="n">r</span> <span class="bp">+</span> <span class="n">b</span> <span class="bp">*</span> <span class="n">q</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">H</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">gcd</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="n">l</span> <span class="bp">+</span> <span class="o">(</span><span class="n">gcd</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="n">k</span><span class="o">)</span> <span class="bp">*</span> <span class="n">q</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="bp">&#8592;</span> <span class="n">hk</span><span class="o">,</span> <span class="bp">&#8592;</span> <span class="n">hl</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">gcd</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="o">(</span><span class="n">l</span> <span class="bp">+</span> <span class="n">k</span> <span class="bp">*</span> <span class="n">q</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b &lt; 0`</span>
    <span class="k">have</span> <span class="n">IH1</span> <span class="o">:=</span> <span class="n">gcd_dvd_left</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="c1">-- inductive hypothesis</span>
    <span class="k">have</span> <span class="n">IH2</span> <span class="o">:=</span> <span class="n">gcd_dvd_right</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="c1">-- inductive hypothesis</span>
    <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">k</span><span class="o">,</span> <span class="n">hk</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">IH1</span>
    <span class="n">obtain</span> <span class="o">&#10216;</span><span class="n">l</span><span class="o">,</span> <span class="n">hl</span><span class="o">&#10217;</span> <span class="o">:=</span> <span class="n">IH2</span>
    <span class="k">have</span> <span class="n">H</span> <span class="o">:=</span> <span class="n">fmod_add_fdiv</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span>
    <span class="n">set</span> <span class="n">q</span> <span class="o">:=</span> <span class="n">fdiv</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span>
    <span class="n">set</span> <span class="n">r</span> <span class="o">:=</span> <span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span>
    <span class="n">use</span> <span class="n">l</span> <span class="bp">-</span> <span class="n">k</span> <span class="bp">*</span> <span class="n">q</span>
    <span class="k">calc</span> <span class="n">a</span> <span class="bp">=</span> <span class="n">r</span> <span class="bp">+</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span> <span class="bp">*</span> <span class="n">q</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">H</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">gcd</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="n">l</span> <span class="bp">+</span> <span class="o">(</span><span class="bp">-</span> <span class="o">(</span><span class="n">gcd</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="n">k</span><span class="o">))</span> <span class="bp">*</span> <span class="n">q</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="bp">&#8592;</span> <span class="n">hk</span><span class="o">,</span> <span class="bp">&#8592;</span> <span class="n">hl</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">gcd</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="o">(</span><span class="n">l</span> <span class="bp">-</span> <span class="n">k</span> <span class="bp">*</span> <span class="n">q</span><span class="o">)</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `0 &#8804; a`</span>
    <span class="n">use</span> <span class="mi">1</span>
    <span class="n">ring</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `a &lt; 0`</span>
    <span class="n">use</span> <span class="bp">-</span><span class="mi">1</span>
    <span class="n">ring</span>

<span class="kd">end</span>
<span class="n">termination_by</span> <span class="n">gcd_dvd_right</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">b</span> <span class="bp">;</span> <span class="n">gcd_dvd_left</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">b</span>
</pre></div>
</div>
<p>There is a new tactic in the above proof: <code class="docutils literal notranslate"><span class="pre">set</span></code>, which introduce a short name for a long
expression (typically one which occurs frequently and you are tired of typing out in full).  Notice
how the goal state changes before and after its use.</p>
</section>
<section id="bezout-def">
<span id="id39"></span><h3><span class="section-number">6.7.4. </span>Definition<a class="headerlink" href="#bezout-def" title="Permalink to this headline">&#61633;</a></h3>
<p>The process described in <a class="reference internal" href="#euclidean-algorithm-def"><span class="std std-numref">Definition 6.7.1</span></a> is generally called the
<em>Euclidean algorithm</em>.  A process called the <em>extended Euclidean algorithm</em> is used to compute two
other functions, which we will call <span class="math notranslate nohighlight">\(L(a,b)\)</span> and <span class="math notranslate nohighlight">\(R(a,b)\)</span>, at the same time as
<span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span>.</p>
<div class="admonition-definition admonition">
<p class="admonition-title">Definition</p>
<p>The functions <span class="math notranslate nohighlight">\(L\)</span> and <span class="math notranslate nohighlight">\(R\)</span> of two integers are defined mutually recursively as follows:</p>
<blockquote>
<div><div class="math notranslate nohighlight">
\[\begin{split}L(a,b)&amp;=
\begin{cases}
  R(b,\operatorname{mod}(a,b)) &amp; 0&lt; b\\
  R(b,\operatorname{mod}(a,-b)) &amp; b&lt;0\\
  1 &amp; b=0\text{ and }0\le a\\
  -1 &amp; b=0\text{ and }a &lt;0.
\end{cases}  \\
R(a,b)&amp;=
\begin{cases}
  L(b,\operatorname{mod}(a,b))-\operatorname{div}(a,b)R(b,\operatorname{mod}(a,b)) &amp; 0&lt; b\\
  L(b,\operatorname{mod}(a,-b))+\operatorname{div}(a,-b)R(b,\operatorname{mod}(a,-b)) &amp; b&lt;0\\
  0 &amp; b=0.
\end{cases}  \\\end{split}\]</div>
</div></blockquote>
</div>
<p>Let&#8217;s practice evaluating the <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span>, <span class="math notranslate nohighlight">\(L(a,b)\)</span> and <span class="math notranslate nohighlight">\(R(a,b)\)</span>
functions jointly, on the same example (<span class="math notranslate nohighlight">\(a=-21\)</span>, <span class="math notranslate nohighlight">\(b=15\)</span>) as
in <a class="reference internal" href="#euclidean-algorithm-def"><span class="std std-numref">Definition 6.7.1</span></a>.</p>
<p>Since we will need both <span class="math notranslate nohighlight">\(\operatorname{div}\)</span> and <span class="math notranslate nohighlight">\(\operatorname{mod}\)</span> down the
recursion, it is convenient to calculate them jointly in advance:</p>
<div class="math notranslate nohighlight">
\[\begin{split}-21&amp;=9+15\cdot -2\\
15&amp;=6+9\cdot 1\\
9&amp;=3+6\cdot 1\\
6&amp;=0+3\cdot 2.\end{split}\]</div>
<p>(This table is a shorthand record that <span class="math notranslate nohighlight">\(\operatorname{div}(-21,15)=-2\)</span> and
<span class="math notranslate nohighlight">\(\operatorname{mod}(-21,15)=9\)</span>, and so on.)  We conclude immediately that</p>
<div class="math notranslate nohighlight">
\[\begin{split}\operatorname{gcd}(-21,15)&amp;=\operatorname{gcd}(15,9)\\
&amp;=\operatorname{gcd}(9,6)\\
&amp;=\operatorname{gcd}(6,3)\\
&amp;=\operatorname{gcd}(3,0)\\
&amp;=3,\end{split}\]</div>
<p>as before.  To calculate <span class="math notranslate nohighlight">\(L(a,b)\)</span> and <span class="math notranslate nohighlight">\(R(a,b)\)</span>, it is convenient to work backwards:</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}L(3,0)&amp;=1
&amp;
\qquad R(3,0)&amp;=0\\L(6,3)&amp;=R(3,0)
&amp;
\qquad R(6,3)&amp;=L(3,0)-\operatorname{div}(6,3)R(3,0)\\&amp;=0
&amp;
\qquad&amp;=1-2\cdot 0\\&amp;
&amp;
\qquad&amp;=1\\L(9,6)&amp;=R(6,3)
&amp;
\qquad R(9,6)&amp;=L(6,3)-\operatorname{div}(9,6)R(6,3)\\&amp;=1
&amp;
\qquad&amp;=0-1\cdot 1\\&amp;
&amp;
\qquad&amp;=-1\\L(15,9)&amp;=R(9,6)
&amp;
\qquad R(15,9)&amp;=L(9,6)-\operatorname{div}(15,9)R(9,6)\\&amp;=-1
&amp;
\qquad&amp;=1-1\cdot -1\\&amp;
&amp;
\qquad&amp;=2\\L(-21,15)&amp;=R(15,9)
&amp;
\qquad R(-21,15)&amp;=L(15,9)-\operatorname{div}(-21,15)R(15,9)\\&amp;=2
&amp;
\qquad&amp;=-1-(-2)\cdot 2\\&amp;
&amp;
\qquad&amp;=3\end{aligned}\end{align} \]</div>
<p>In Lean this mutual-recursion definition looks similar to the mutual-induction proof in
<a class="reference internal" href="#common-divisor"><span class="std std-numref">Example 6.7.3</span></a>; like that example it is enclosed in a block marked
<code class="docutils literal notranslate"><span class="pre">mutual</span></code>.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">mutual</span>

<span class="kd">def</span> <span class="n">L</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">&#8484;</span> <span class="o">:=</span>
  <span class="k">if</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">b</span> <span class="k">then</span>
    <span class="n">R</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span>
  <span class="k">else</span> <span class="k">if</span> <span class="n">b</span> <span class="bp">&lt;</span> <span class="mi">0</span> <span class="k">then</span>
    <span class="n">R</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span>
  <span class="k">else</span> <span class="k">if</span> <span class="mi">0</span> <span class="bp">&#8804;</span> <span class="n">a</span> <span class="k">then</span>
    <span class="mi">1</span>
  <span class="k">else</span>
    <span class="bp">-</span><span class="mi">1</span>

<span class="kd">def</span> <span class="n">R</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">&#8484;</span> <span class="o">:=</span>
  <span class="k">if</span> <span class="mi">0</span> <span class="bp">&lt;</span> <span class="n">b</span> <span class="k">then</span>
    <span class="n">L</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="bp">-</span> <span class="o">(</span><span class="n">fdiv</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="bp">*</span> <span class="n">R</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span>
  <span class="k">else</span> <span class="k">if</span> <span class="n">b</span> <span class="bp">&lt;</span> <span class="mi">0</span> <span class="k">then</span>
    <span class="n">L</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="bp">+</span> <span class="o">(</span><span class="n">fdiv</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="bp">*</span> <span class="n">R</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span>
  <span class="k">else</span>
    <span class="mi">0</span>

<span class="kd">end</span>
<span class="n">termination_by</span> <span class="n">L</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">b</span> <span class="bp">;</span> <span class="n">R</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">b</span>
</pre></div>
</div>
<p>Sanity check: does the Lean definition agree with our computation by hand of <span class="math notranslate nohighlight">\(L(-21,15)\)</span> and
<span class="math notranslate nohighlight">\(R(-21,15)\)</span>?</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="k">#eval</span> <span class="n">L</span> <span class="o">(</span><span class="bp">-</span><span class="mi">21</span><span class="o">)</span> <span class="mi">15</span> <span class="c1">-- infoview displays `2`</span>
<span class="k">#eval</span> <span class="n">R</span> <span class="o">(</span><span class="bp">-</span><span class="mi">21</span><span class="o">)</span> <span class="mi">15</span> <span class="c1">-- infoview displays `3`</span>
</pre></div>
</div>
</section>
<section id="bezout-gcd-recursive">
<span id="id40"></span><h3><span class="section-number">6.7.5. </span>Example<a class="headerlink" href="#bezout-gcd-recursive" title="Permalink to this headline">&#61633;</a></h3>
<p>The reason for making the definitions <span class="math notranslate nohighlight">\(L(a,b)\)</span> and <span class="math notranslate nohighlight">\(R(a,b)\)</span> is that they satisfy the
following identity.</p>
<div class="admonition-theorem admonition">
<p class="admonition-title">Theorem</p>
<p>For all integers <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>,</p>
<div class="math notranslate nohighlight">
\[L(a,b)a+R(a,b)b=\operatorname{gcd}(a,b).\]</div>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>We prove this by strong induction on <span class="math notranslate nohighlight">\(b\)</span>.   Suppose that for all
integers <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> with <span class="math notranslate nohighlight">\(|y|&lt;|b|\)</span>, it is true that
<span class="math notranslate nohighlight">\(0 \le  \operatorname{gcd}(x, y)\)</span>.</p>
<p><strong>Case 1</strong> (<span class="math notranslate nohighlight">\(0&lt;b\)</span>): Let <span class="math notranslate nohighlight">\(q=\operatorname{div}(a,b)\)</span> and let
<span class="math notranslate nohighlight">\(r=\operatorname{mod}(a,b)\)</span>, so that <span class="math notranslate nohighlight">\(a=r+bq\)</span> (by <a class="reference internal" href="#mod-add-div"><span class="std std-numref">Example 6.6.2</span></a>).</p>
<p>Then by the recurrence definitions,</p>
<div class="math notranslate nohighlight">
\[\begin{split}\operatorname{gcd}(a,b)&amp;=\operatorname{gcd}(b,r)\\
L(a,b)&amp;=R(b,r)\\
R(a,b)&amp;=L(b,r)-qR(b,r)\end{split}\]</div>
<p>and by the inductive hypothesis <span class="math notranslate nohighlight">\(L(b,r)b+R(b,r)r=\operatorname{gcd}(b,r)\)</span>.  So</p>
<div class="math notranslate nohighlight">
\[\begin{split}L(a,b)a+R(a,b)b
&amp;=R(b,r)a+\left(L(b,r)-qR(b,r)\right)b\\
&amp;=R(b,r)(r+bq)+\left(L(b,r)-qR(b,r)\right)b\\
&amp;=R(b,r)r+L(b,r)b\\
&amp;=\operatorname{gcd}(b,r)\\
&amp;=\operatorname{gcd}(a,b).\end{split}\]</div>
<p><strong>Case 2</strong> (<span class="math notranslate nohighlight">\(b&lt;0\)</span>): Let <span class="math notranslate nohighlight">\(q=\operatorname{div}(a,-b)\)</span> and let
<span class="math notranslate nohighlight">\(r=\operatorname{mod}(a,-b)\)</span>, so that <span class="math notranslate nohighlight">\(a=r+(-b)q\)</span> (by
<a class="reference internal" href="#mod-add-div"><span class="std std-numref">Example 6.6.2</span></a>).</p>
<p>Then by the recurrence definitions,</p>
<div class="math notranslate nohighlight">
\[\begin{split}\operatorname{gcd}(a,b)&amp;=\operatorname{gcd}(b,r)\\
L(a,b)&amp;=R(b,r)\\
R(a,b)&amp;=L(b,r)+qR(b,r)\end{split}\]</div>
<p>and by the inductive hypothesis <span class="math notranslate nohighlight">\(L(b,r)b+R(b,r)r=\operatorname{gcd}(b,r)\)</span>.  So</p>
<div class="math notranslate nohighlight">
\[\begin{split}L(a,b)a+R(a,b)b
&amp;=R(b,r)a+\left(L(b,r)+qR(b,r)\right)b\\
&amp;=R(b,r)(r+(-b)q)+\left(L(b,r)+qR(b,r)\right)b\\
&amp;=R(b,r)r+L(b,r)b\\
&amp;=\operatorname{gcd}(b,r)\\
&amp;=\operatorname{gcd}(a,b).\end{split}\]</div>
<p><strong>Case 3</strong> (<span class="math notranslate nohighlight">\(b=0\)</span>, <span class="math notranslate nohighlight">\(0\le a\)</span>): Then by the recurrence definitions,
<span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)=a\)</span>, <span class="math notranslate nohighlight">\(L(a,b)=1\)</span> and <span class="math notranslate nohighlight">\(R(a,b)=0\)</span>, so</p>
<div class="math notranslate nohighlight">
\[\begin{split}L(a,b)a+R(a,b)b
&amp;=1\cdot a+0\cdot b\\
&amp;=a\\
&amp;=\operatorname{gcd}(a,b).\end{split}\]</div>
<p><strong>Case 4</strong> (<span class="math notranslate nohighlight">\(b=0\)</span>, <span class="math notranslate nohighlight">\(a&lt;0\)</span>): Then by the recurrence definitions,
<span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)=-a\)</span>, <span class="math notranslate nohighlight">\(L(a,b)=-1\)</span> and <span class="math notranslate nohighlight">\(R(a,b)=0\)</span>, so</p>
<div class="math notranslate nohighlight">
\[\begin{split}L(a,b)a+R(a,b)b
&amp;=-1\cdot -a+0\cdot b\\
&amp;=a\\
&amp;=\operatorname{gcd}(a,b).\end{split}\]</div>
</div>
<p>Here is the same proof in Lean.</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">L_mul_add_R_mul</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="n">L</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">+</span> <span class="n">R</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">*</span> <span class="n">b</span> <span class="bp">=</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">rw</span> <span class="o">[</span><span class="n">R</span><span class="o">,</span> <span class="n">L</span><span class="o">,</span> <span class="n">gcd</span><span class="o">]</span>
  <span class="n">split_ifs</span> <span class="k">with</span> <span class="n">h1</span> <span class="n">h2</span> <span class="bp">&lt;;&gt;</span> <span class="n">push_neg</span> <span class="n">at</span> <span class="bp">*</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `0 &lt; b`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">L_mul_add_R_mul</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span><span class="o">)</span> <span class="c1">-- inductive hypothesis</span>
    <span class="k">have</span> <span class="n">H</span> <span class="o">:</span> <span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">+</span> <span class="n">b</span> <span class="bp">*</span> <span class="n">fdiv</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=</span> <span class="n">a</span> <span class="o">:=</span> <span class="n">fmod_add_fdiv</span> <span class="n">a</span> <span class="n">b</span>
    <span class="n">set</span> <span class="n">q</span> <span class="o">:=</span> <span class="n">fdiv</span> <span class="n">a</span> <span class="n">b</span>
    <span class="n">set</span> <span class="n">r</span> <span class="o">:=</span> <span class="n">fmod</span> <span class="n">a</span> <span class="n">b</span>
    <span class="k">calc</span> <span class="n">R</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">+</span> <span class="o">(</span><span class="n">L</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">-</span> <span class="n">q</span> <span class="bp">*</span> <span class="n">R</span> <span class="n">b</span> <span class="n">r</span><span class="o">)</span> <span class="bp">*</span> <span class="n">b</span>
        <span class="bp">=</span> <span class="n">R</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="o">(</span><span class="n">r</span> <span class="bp">+</span> <span class="n">b</span> <span class="bp">*</span> <span class="n">q</span><span class="o">)</span> <span class="bp">+</span> <span class="o">(</span><span class="n">L</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">-</span> <span class="n">q</span> <span class="bp">*</span> <span class="n">R</span> <span class="n">b</span> <span class="n">r</span><span class="o">)</span> <span class="bp">*</span> <span class="n">b</span><span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">H</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">L</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="n">b</span> <span class="bp">+</span> <span class="n">R</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="n">r</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">gcd</span> <span class="n">b</span> <span class="n">r</span> <span class="o">:=</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b &lt; 0`</span>
    <span class="k">have</span> <span class="n">IH</span> <span class="o">:=</span> <span class="n">L_mul_add_R_mul</span> <span class="n">b</span> <span class="o">(</span><span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">))</span> <span class="c1">-- inductive hypothesis</span>
    <span class="k">have</span> <span class="n">H</span> <span class="o">:</span> <span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span> <span class="bp">+</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span> <span class="bp">*</span> <span class="n">fdiv</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span> <span class="bp">=</span> <span class="n">a</span> <span class="o">:=</span> <span class="n">fmod_add_fdiv</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span>
    <span class="n">set</span> <span class="n">q</span> <span class="o">:=</span> <span class="n">fdiv</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span>
    <span class="n">set</span> <span class="n">r</span> <span class="o">:=</span> <span class="n">fmod</span> <span class="n">a</span> <span class="o">(</span><span class="bp">-</span><span class="n">b</span><span class="o">)</span>
    <span class="k">calc</span>  <span class="n">R</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">+</span> <span class="o">(</span><span class="n">L</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">+</span> <span class="n">q</span> <span class="bp">*</span> <span class="n">R</span> <span class="n">b</span> <span class="n">r</span><span class="o">)</span> <span class="bp">*</span> <span class="n">b</span>
        <span class="bp">=</span>  <span class="n">R</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="o">(</span><span class="n">r</span> <span class="bp">+</span> <span class="bp">-</span><span class="n">b</span> <span class="bp">*</span> <span class="n">q</span><span class="o">)</span> <span class="bp">+</span> <span class="o">(</span><span class="n">L</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">+</span> <span class="n">q</span> <span class="bp">*</span> <span class="n">R</span> <span class="n">b</span> <span class="n">r</span><span class="o">)</span> <span class="bp">*</span> <span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">rw</span> <span class="o">[</span><span class="n">H</span><span class="o">]</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">L</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="n">b</span> <span class="bp">+</span> <span class="n">R</span> <span class="n">b</span> <span class="n">r</span> <span class="bp">*</span> <span class="n">r</span> <span class="o">:=</span> <span class="kd">by</span> <span class="n">ring</span>
      <span class="n">_</span> <span class="bp">=</span> <span class="n">gcd</span> <span class="n">b</span> <span class="n">r</span> <span class="o">:=</span> <span class="n">IH</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `0 &#8804; a`</span>
    <span class="n">ring</span>
  <span class="bp">&#183;</span> <span class="c1">-- case `b = 0`, `a &lt; 0`</span>
    <span class="n">ring</span>
<span class="n">termination_by</span> <span class="n">L_mul_add_R_mul</span> <span class="n">a</span> <span class="n">b</span> <span class="bp">=&gt;</span> <span class="n">b</span>
</pre></div>
</div>
</section>
<section id="bezout-gcd">
<span id="id41"></span><h3><span class="section-number">6.7.6. </span>Example<a class="headerlink" href="#bezout-gcd" title="Permalink to this headline">&#61633;</a></h3>
<p>We proved in <a class="reference internal" href="#bezout-gcd-recursive"><span class="std std-numref">Example 6.7.5</span></a> that for any integers <span class="math notranslate nohighlight">\(a\)</span> and
<span class="math notranslate nohighlight">\(b\)</span>, the integers <span class="math notranslate nohighlight">\(L(a,b)\)</span> and  <span class="math notranslate nohighlight">\(R(a,b)\)</span> satisfy</p>
<div class="math notranslate nohighlight">
\[L(a,b)a+R(a,b)b=\operatorname{gcd}(a,b).\]</div>
<p>For example, <span class="math notranslate nohighlight">\(L(7,5)=-2\)</span> and <span class="math notranslate nohighlight">\(R(7,5)=3\)</span> and <span class="math notranslate nohighlight">\(\operatorname{gcd}(7,5)=1\)</span></p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="k">#eval</span> <span class="n">L</span> <span class="mi">7</span> <span class="mi">5</span> <span class="c1">-- infoview displays `-2`</span>
<span class="k">#eval</span> <span class="n">R</span> <span class="mi">7</span> <span class="mi">5</span> <span class="c1">-- infoview displays `3`</span>
<span class="k">#eval</span> <span class="n">gcd</span> <span class="mi">7</span> <span class="mi">5</span> <span class="c1">-- infoview displays `1`</span>
</pre></div>
</div>
<p>and <span class="math notranslate nohighlight">\((-2) \cdot 7 + 3 \cdot 5 = 1\)</span>.</p>
<p>But it is interesting to note that these are generally not the only pair of integers with that
property.  For example,</p>
<div class="math notranslate nohighlight">
\[\begin{split}3 \cdot 7 + (-4) \cdot 5 &amp;= 1\\
(-7) \cdot 7 + 10 \cdot 5 &amp;= 1\\
8 \cdot 7 + (-11) \cdot 5 &amp;= 1\\
\ldots\end{split}\]</div>
<p>In applications, it is common to need only the property, not the particular construction via
<span class="math notranslate nohighlight">\(L(a,b)\)</span> and  <span class="math notranslate nohighlight">\(R(a,b)\)</span>.  So we record this separately.  This fact is known as
<em>B&#233;zout&#8217;s identity</em>.</p>
<div class="admonition-corollary-bezout-s-identity admonition">
<p class="admonition-title">Corollary (B&#233;zout&#8217;s identity)</p>
<p>Let <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> be integers.  Then there exist integers <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span>, such
that <span class="math notranslate nohighlight">\(xa+yb=\operatorname{gcd}(a,b)\)</span>.</p>
</div>
<div class="admonition-proof admonition">
<p class="admonition-title">Proof</p>
<p>By  <a class="reference internal" href="#bezout-gcd-recursive"><span class="std std-numref">Example 6.7.5</span></a>, the integers  <span class="math notranslate nohighlight">\(L(a,b)\)</span> and  <span class="math notranslate nohighlight">\(R(a,b)\)</span>
have this property.</p>
</div>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">bezout</span> <span class="o">(</span><span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">)</span> <span class="o">:</span> <span class="bp">&#8707;</span> <span class="n">x</span> <span class="n">y</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">,</span> <span class="n">x</span> <span class="bp">*</span> <span class="n">a</span> <span class="bp">+</span> <span class="n">y</span> <span class="bp">*</span> <span class="n">b</span> <span class="bp">=</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="n">use</span> <span class="n">L</span> <span class="n">a</span> <span class="n">b</span><span class="o">,</span> <span class="n">R</span> <span class="n">a</span> <span class="n">b</span>
  <span class="n">apply</span> <span class="n">L_mul_add_R_mul</span>
</pre></div>
</div>
</section>
<section id="id42">
<h3><span class="section-number">6.7.7. </span>Exercises<a class="headerlink" href="#id42" title="Permalink to this headline">&#61633;</a></h3>
<ol class="arabic">
<li><p>Show that <span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span> is not just a <em>common divisor</em> of <span class="math notranslate nohighlight">\(a\)</span> and
<span class="math notranslate nohighlight">\(b\)</span> (see <a class="reference internal" href="#common-divisor"><span class="std std-numref">Example 6.7.3</span></a>), but their <em>greatest common divisor</em>: if
<span class="math notranslate nohighlight">\(d\)</span> is an integer which divides both <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span>, then it divides
<span class="math notranslate nohighlight">\(\operatorname{gcd}(a,b)\)</span>.</p>
<p>This problem does not need an induction; it is a direct corollary of B&#233;zout&#8217;s identity
(<a class="reference internal" href="#bezout-gcd"><span class="std std-numref">Example 6.7.6</span></a>).</p>
<div class="highlight-lean notranslate"><div class="highlight"><pre><span></span><span class="kd">theorem</span> <span class="n">gcd_maximal</span> <span class="o">{</span><span class="n">d</span> <span class="n">a</span> <span class="n">b</span> <span class="o">:</span> <span class="n">&#8484;</span><span class="o">}</span> <span class="o">(</span><span class="n">ha</span> <span class="o">:</span> <span class="n">d</span> <span class="bp">&#8739;</span> <span class="n">a</span><span class="o">)</span> <span class="o">(</span><span class="n">hb</span> <span class="o">:</span> <span class="n">d</span> <span class="bp">&#8739;</span> <span class="n">b</span><span class="o">)</span> <span class="o">:</span> <span class="n">d</span> <span class="bp">&#8739;</span> <span class="n">gcd</span> <span class="n">a</span> <span class="n">b</span> <span class="o">:=</span> <span class="kd">by</span>
  <span class="gr">sorry</span>
</pre></div>
</div>
</li>
</ol>
</section>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="05_Logic.html" class="btn btn-neutral float-left" title="5. Logic" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="07_Number_Theory.html" class="btn btn-neutral float-right" title="7. Number theory" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2022-2024, Heather Macbeth.  All rights reserved.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>